view src/de/mpiwg/itgroup/eSciDoc/echoObjects/ECHORessource.java @ 0:c6929e63b0b8

first import
author dwinter
date Wed, 24 Nov 2010 16:52:07 +0100
parents
children 58b52df9763c
line wrap: on
line source

package de.mpiwg.itgroup.eSciDoc.echoObjects;

import java.io.IOException;
import java.net.URI;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import org.apache.http.Header;
import org.apache.http.HttpException;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpContext;

import de.mpiwg.itgroup.eSciDoc.Tools.EScidocBasicHandler;
import de.mpiwg.itgroup.eSciDoc.Tools.EScidocTools;
import de.mpiwg.itgroup.metadataManager.pid.DCTransformer;

public class ECHORessource  extends ECHOObject{

	
	public String archivePath;
	public String metaData ="";
	public String fullText ="";
	private String textServletUrl;
	private String dirInfoUrl;
	public String link;
	

	public ECHORessource() throws IOException{
		super();
		CMM ="/cmm/content-model/escidoc:11003";
		Properties echoProperties = new Properties();
		echoProperties.load(getClass().getResourceAsStream("/de/mpiwg/itgroup/eSciDoc/config/echo.properties"));
		
		textServletUrl = (String) echoProperties.get("textServletUrl");
		dirInfoUrl = (String) echoProperties.get("dirInfoUrl");
		
	}

	
	public ECHORessource(String name, String archivePath, String echoUrl) throws IOException {
		this();
		CMM ="/cmm/content-model/escidoc:11003";
		this.name = name;
		this.archivePath = archivePath;
		this.echoUrl = echoUrl;
		this.link = getLinkFromUrl(echoUrl);
		
		
	}

	/** Holt die URL auf die das ECHO Objekt redirected
	 * @param echoUrl
	 * @return
	 */
	private String getLinkFromUrl(String echoUrl) {
		if (echoUrl==null)
			return null;
		HttpContext localContext = new BasicHttpContext();
		HttpClient hc = new DefaultHttpClient();
		HttpGet get = new HttpGet(echoUrl);
		HttpResponse response;
		HttpUriRequest req;
		HttpHost target;
		
		try {
			response = hc.execute(get,localContext);
			req = (HttpUriRequest) localContext.getAttribute(
			        ExecutionContext.HTTP_REQUEST);
			target = (HttpHost) localContext.getAttribute(
			        ExecutionContext.HTTP_TARGET_HOST);


		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		URI ret = req.getURI();
		
		return target.toString()+ret.toString();
	}


	public String toString(){
		String format="NAME: %s; archivePAth: %s; metaData: %s; fullText %s; url %s";
		
		return String.format(format, name,archivePath,metaData,fullText,echoUrl);
		
	}
	

	/**
	 * Baue Metadatalink um, dass er auf das Servlet zeigt.
	 * @param ml
	 * @return
	 */
	public String correctML(String ml) {
		Pattern p = Pattern.compile("experimental/(.*)");
		Matcher m = p.matcher(ml);
		String pf;
		if (m.find())
			pf = "experimental/" + m.group(1);
		else {
			p = Pattern.compile("permanent/(.*)");
			m = p.matcher(ml);
			if (m.find())
				pf = "permanent/" + m.group(1);
			else
				return ml;
		}
		return textServletUrl + pf;
	}

	static public String correct(String ml){
		Pattern p = Pattern.compile("experimental/(.*)");
		Matcher m = p.matcher(ml);
		String pf;
		if (m.find())
			pf = "experimental/" + m.group(1);
		else {
			p = Pattern.compile("permanent/(.*)");
			m = p.matcher(ml);
			if (m.find())
				pf = "permanent/" + m.group(1);
			else
				return ml;
		}
		return pf;
	}
	
	
	public String getImageFolderPath() {
		DCTransformer trans = new DCTransformer(metaData);
		String path = trans.getImagePathFromIndexMeta();
		if (path==null || path.equals("")){
			path=archivePath+"/pageimg";
		} else {
			path=archivePath+"/"+path;
		}
		
		String testPath=correct(path); // get rid of everything before eperimental or permanent
		if(testPath(testPath))
			return path;
		
		return null;
	}


	private boolean testPath(String path) {
		HttpClient client = new DefaultHttpClient();
		HttpGet get;
		try {
		get = new HttpGet(dirInfoUrl+path);
		} catch (RuntimeException e){
			e.printStackTrace();
			return false;
		}
		
		try {
			HttpResponse response = client.execute(get);
			String body = EScidocBasicHandler.convertStreamToString(response.getEntity().getContent());;
			
			Pattern p = Pattern.compile("<dir>(.*)</dir>",Pattern.DOTALL);
			Matcher m = p.matcher(body);
			if (m.find()){ // dir body leer
				if (m.group(1).equals(""))
					return false;
				else
					return true;
			}
			
	
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return false;
	}
}