0
|
1 package de.mpiwg.itgroup.eSciDoc.echoObjects;
|
|
2
|
|
3 import java.io.IOException;
|
|
4 import java.net.URI;
|
|
5 import java.util.Properties;
|
|
6 import java.util.regex.Matcher;
|
|
7 import java.util.regex.Pattern;
|
|
8
|
|
9
|
|
10 import org.apache.http.Header;
|
|
11 import org.apache.http.HttpException;
|
|
12 import org.apache.http.HttpHost;
|
|
13 import org.apache.http.HttpResponse;
|
|
14 import org.apache.http.client.ClientProtocolException;
|
|
15 import org.apache.http.client.HttpClient;
|
|
16 import org.apache.http.client.methods.HttpGet;
|
|
17 import org.apache.http.client.methods.HttpUriRequest;
|
|
18 import org.apache.http.impl.client.DefaultHttpClient;
|
|
19 import org.apache.http.protocol.BasicHttpContext;
|
|
20 import org.apache.http.protocol.ExecutionContext;
|
|
21 import org.apache.http.protocol.HttpContext;
|
|
22
|
|
23 import de.mpiwg.itgroup.eSciDoc.Tools.EScidocBasicHandler;
|
|
24 import de.mpiwg.itgroup.eSciDoc.Tools.EScidocTools;
|
|
25 import de.mpiwg.itgroup.metadataManager.pid.DCTransformer;
|
|
26
|
|
27 public class ECHORessource extends ECHOObject{
|
|
28
|
|
29
|
|
30 public String archivePath;
|
|
31 public String metaData ="";
|
|
32 public String fullText ="";
|
|
33 private String textServletUrl;
|
|
34 private String dirInfoUrl;
|
|
35 public String link;
|
|
36
|
|
37
|
|
38 public ECHORessource() throws IOException{
|
|
39 super();
|
|
40 CMM ="/cmm/content-model/escidoc:11003";
|
|
41 Properties echoProperties = new Properties();
|
|
42 echoProperties.load(getClass().getResourceAsStream("/de/mpiwg/itgroup/eSciDoc/config/echo.properties"));
|
|
43
|
|
44 textServletUrl = (String) echoProperties.get("textServletUrl");
|
|
45 dirInfoUrl = (String) echoProperties.get("dirInfoUrl");
|
|
46
|
|
47 }
|
|
48
|
|
49
|
|
50 public ECHORessource(String name, String archivePath, String echoUrl) throws IOException {
|
|
51 this();
|
|
52 CMM ="/cmm/content-model/escidoc:11003";
|
|
53 this.name = name;
|
|
54 this.archivePath = archivePath;
|
|
55 this.echoUrl = echoUrl;
|
|
56 this.link = getLinkFromUrl(echoUrl);
|
|
57
|
|
58
|
|
59 }
|
|
60
|
|
61 /** Holt die URL auf die das ECHO Objekt redirected
|
|
62 * @param echoUrl
|
|
63 * @return
|
|
64 */
|
|
65 private String getLinkFromUrl(String echoUrl) {
|
|
66 if (echoUrl==null)
|
|
67 return null;
|
|
68 HttpContext localContext = new BasicHttpContext();
|
|
69 HttpClient hc = new DefaultHttpClient();
|
|
70 HttpGet get = new HttpGet(echoUrl);
|
|
71 HttpResponse response;
|
|
72 HttpUriRequest req;
|
|
73 HttpHost target;
|
|
74
|
|
75 try {
|
|
76 response = hc.execute(get,localContext);
|
|
77 req = (HttpUriRequest) localContext.getAttribute(
|
|
78 ExecutionContext.HTTP_REQUEST);
|
|
79 target = (HttpHost) localContext.getAttribute(
|
|
80 ExecutionContext.HTTP_TARGET_HOST);
|
|
81
|
|
82
|
|
83 } catch (ClientProtocolException e) {
|
|
84 // TODO Auto-generated catch block
|
|
85 e.printStackTrace();
|
|
86 return "";
|
|
87 } catch (IOException e) {
|
|
88 // TODO Auto-generated catch block
|
|
89 e.printStackTrace();
|
|
90 return "";
|
|
91 }
|
|
92 URI ret = req.getURI();
|
|
93
|
|
94 return target.toString()+ret.toString();
|
|
95 }
|
|
96
|
|
97
|
|
98 public String toString(){
|
|
99 String format="NAME: %s; archivePAth: %s; metaData: %s; fullText %s; url %s";
|
|
100
|
|
101 return String.format(format, name,archivePath,metaData,fullText,echoUrl);
|
|
102
|
|
103 }
|
|
104
|
|
105
|
|
106 /**
|
|
107 * Baue Metadatalink um, dass er auf das Servlet zeigt.
|
|
108 * @param ml
|
|
109 * @return
|
|
110 */
|
|
111 public String correctML(String ml) {
|
|
112 Pattern p = Pattern.compile("experimental/(.*)");
|
|
113 Matcher m = p.matcher(ml);
|
|
114 String pf;
|
|
115 if (m.find())
|
|
116 pf = "experimental/" + m.group(1);
|
|
117 else {
|
|
118 p = Pattern.compile("permanent/(.*)");
|
|
119 m = p.matcher(ml);
|
|
120 if (m.find())
|
|
121 pf = "permanent/" + m.group(1);
|
|
122 else
|
|
123 return ml;
|
|
124 }
|
|
125 return textServletUrl + pf;
|
|
126 }
|
|
127
|
|
128 static public String correct(String ml){
|
|
129 Pattern p = Pattern.compile("experimental/(.*)");
|
|
130 Matcher m = p.matcher(ml);
|
|
131 String pf;
|
|
132 if (m.find())
|
|
133 pf = "experimental/" + m.group(1);
|
|
134 else {
|
|
135 p = Pattern.compile("permanent/(.*)");
|
|
136 m = p.matcher(ml);
|
|
137 if (m.find())
|
|
138 pf = "permanent/" + m.group(1);
|
|
139 else
|
|
140 return ml;
|
|
141 }
|
|
142 return pf;
|
|
143 }
|
|
144
|
|
145
|
|
146 public String getImageFolderPath() {
|
|
147 DCTransformer trans = new DCTransformer(metaData);
|
|
148 String path = trans.getImagePathFromIndexMeta();
|
|
149 if (path==null || path.equals("")){
|
|
150 path=archivePath+"/pageimg";
|
|
151 } else {
|
|
152 path=archivePath+"/"+path;
|
|
153 }
|
|
154
|
|
155 String testPath=correct(path); // get rid of everything before eperimental or permanent
|
|
156 if(testPath(testPath))
|
|
157 return path;
|
|
158
|
|
159 return null;
|
|
160 }
|
|
161
|
|
162
|
|
163 private boolean testPath(String path) {
|
|
164 HttpClient client = new DefaultHttpClient();
|
|
165 HttpGet get;
|
|
166 try {
|
|
167 get = new HttpGet(dirInfoUrl+path);
|
|
168 } catch (RuntimeException e){
|
|
169 e.printStackTrace();
|
|
170 return false;
|
|
171 }
|
|
172
|
|
173 try {
|
|
174 HttpResponse response = client.execute(get);
|
|
175 String body = EScidocBasicHandler.convertStreamToString(response.getEntity().getContent());;
|
|
176
|
|
177 Pattern p = Pattern.compile("<dir>(.*)</dir>",Pattern.DOTALL);
|
|
178 Matcher m = p.matcher(body);
|
|
179 if (m.find()){ // dir body leer
|
|
180 if (m.group(1).equals(""))
|
|
181 return false;
|
|
182 else
|
|
183 return true;
|
|
184 }
|
|
185
|
|
186
|
|
187 } catch (IOException e) {
|
|
188 // TODO Auto-generated catch block
|
|
189 e.printStackTrace();
|
|
190 }
|
|
191 return false;
|
|
192 }
|
|
193 }
|