view src/de/mpiwg/itgroup/metadataManager/client/MetadataClient.java @ 1:2267d8c80a99

intial
author dwinter
date Sun, 23 Oct 2011 21:29:45 +0200
parents
children
line wrap: on
line source

package de.mpiwg.itgroup.metadataManager.client;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.xml.parsers.SAXParser;

import org.apache.http.HttpEntity;
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.utils.URIUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;
import org.json.JSONObject;
import org.json.JSONTokener;

import org.restlet.data.MediaType;
import org.restlet.representation.Representation;
import org.restlet.resource.ClientResource;

public class MetadataClient {

	private String serverUrl;

	public MetadataClient(String string) {
		serverUrl = string;
		
	}
 
	public String replaceBIB(String idOfObject, String newBibTag, boolean save) throws URISyntaxException, ClientProtocolException, IOException {
		
		String newIM=null;
		SAXBuilder sb = new SAXBuilder();		
		
		String url = serverUrl+"indexMeta/permanent/library/"+idOfObject;
		
		HttpClient httpclient = new DefaultHttpClient();
	
		HttpGet httpget = new HttpGet(new URI(url));
		
		HttpResponse response = httpclient.execute(httpget);
		HttpEntity entity = response.getEntity();
		
		try {
			if (entity != null) {
			    InputStream instream = (InputStream) entity.getContent();
			    Document dom = sb.build(instream);

			    XPath xp =XPath.newInstance("//bib");
			    Element bibNode = (Element)xp.selectSingleNode(dom);
			    
			    Document doc2 = sb.build(new StringReader(newBibTag));
			    Element newBibRoot=doc2.getRootElement();
			   
			    Element bibPar=(Element) bibNode.getParent();
			    bibPar.removeContent(bibNode);
			    bibPar.addContent((Element) newBibRoot.clone());
			
			    XMLOutputter op = new XMLOutputter();
			    newIM = op.outputString(dom);
			    if (save){
			    	//TODO save the new XML
			    }
			}
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JDOMException e) {
			
			System.err.println("Can't handle:"+ idOfObject);
			e.printStackTrace();
			
		}
		return newIM;
	}

	public String addContext(String indexMeta, String link, String name, Boolean save) throws IOException {
		SAXBuilder sb = new SAXBuilder();	
		InputStream im = new ByteArrayInputStream(indexMeta.getBytes("utf-8"));
		try {
			Document dom =sb.build(im);
			
			XPath xp = XPath.newInstance("//meta");
			Element metaNode=(Element) xp.selectSingleNode(dom);
			
			Element  contextElement = new Element("context");
			
			Element  linkElement = new Element("link");
			linkElement.setText(link);
			Element  nameElement = new Element("name");
			nameElement.setText(name);
			contextElement.addContent(linkElement);
			contextElement.addContent(nameElement);
			
			metaNode.addContent(contextElement);
			
			  XMLOutputter op = new XMLOutputter();
			  String newIM = op.outputString(dom);
			
			  if (save){
			    	//TODO save the new XML
			    }
			return newIM;
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

}