Mercurial > hg > eSciDocImport
comparison src/de/mpiwg/itgroup/eSciDoc/echoObjects/ECHOObject.java @ 0:c6929e63b0b8
first import
author | dwinter |
---|---|
date | Wed, 24 Nov 2010 16:52:07 +0100 |
parents | |
children | 58b52df9763c |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c6929e63b0b8 |
---|---|
1 package de.mpiwg.itgroup.eSciDoc.echoObjects; | |
2 | |
3 import java.io.IOException; | |
4 import java.io.InputStream; | |
5 import java.net.MalformedURLException; | |
6 import java.net.URL; | |
7 import java.util.Properties; | |
8 | |
9 | |
10 import org.apache.http.HttpResponse; | |
11 import org.apache.http.client.HttpClient; | |
12 import org.apache.http.client.methods.HttpPost; | |
13 import org.apache.http.impl.client.DefaultHttpClient; | |
14 import org.apache.log4j.Logger; | |
15 import org.apache.xmlrpc.XmlRpcException; | |
16 import org.apache.xmlrpc.client.XmlRpcClient; | |
17 import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; | |
18 | |
19 import de.mpiwg.itgroup.eSciDoc.Tools.EScidocBasicHandler; | |
20 import de.mpiwg.itgroup.metadataManager.pid.PidGenerator; | |
21 | |
22 public class ECHOObject { | |
23 Logger logger = Logger.getRootLogger(); | |
24 public String CMM; // to be overwritten oder initialized with the CMM of this model. | |
25 public String name; | |
26 public String echoUrl; | |
27 public String pid; | |
28 public String metadataMananagerUrl; | |
29 public String context; | |
30 public String description =""; | |
31 | |
32 public ECHOObject() throws IOException{ | |
33 Properties echoProperties = new Properties(); | |
34 echoProperties.load(getClass().getResourceAsStream("/de/mpiwg/itgroup/eSciDoc/config/echo.properties")); | |
35 | |
36 metadataMananagerUrl = (String) echoProperties.get("metaDataManager"); | |
37 | |
38 } | |
39 | |
40 public String getOrCreatePID() throws Exception { | |
41 Object[] parameters = new Object[] {}; | |
42 | |
43 String pid = null; | |
44 | |
45 //Verbinde mit ECHO | |
46 XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); | |
47 try { | |
48 config.setServerURL(new URL(echoUrl)); | |
49 } catch (MalformedURLException e1) { | |
50 logger.info("PID-Generator: cannot contact"+echoUrl); | |
51 pid = null; | |
52 } | |
53 XmlRpcClient client = new XmlRpcClient(); | |
54 client.setConfig(config); | |
55 // Hole pid aus ECHO | |
56 try { | |
57 pid = (String) client.execute("getPID", parameters); | |
58 } catch (Exception e) { | |
59 logger.info("PID-Generator: cannot contact"+echoUrl); | |
60 pid = null; | |
61 } | |
62 // Falls dort noch keine ist, erzeuge ein neue | |
63 if (pid == null) | |
64 { | |
65 pid = getPid(); | |
66 | |
67 } | |
68 else | |
69 logger.info("PID from ECHO:" + pid); | |
70 | |
71 this.pid=pid; | |
72 return pid; | |
73 } | |
74 | |
75 public String getPid() throws Exception, IOException { | |
76 String pid; | |
77 HttpClient hc = new DefaultHttpClient(); | |
78 HttpPost pm = new HttpPost(metadataMananagerUrl+"/PID"); | |
79 HttpResponse ret; | |
80 try { | |
81 ret = hc.execute(pm); | |
82 | |
83 } catch (IOException e) { | |
84 // TODO Auto-generated catch block | |
85 e.printStackTrace(); | |
86 throw new Exception("cannot generate PID"); | |
87 } | |
88 pid=EScidocBasicHandler.convertStreamToString(ret.getEntity().getContent()); | |
89 if (ret.getStatusLine().getStatusCode()>300){ | |
90 //TODO define better Exceptions | |
91 throw new Exception("cannot generate PID"); | |
92 } | |
93 return pid; | |
94 } | |
95 } |