Mercurial > hg > mpdl-group
comparison software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/escidoc/ESciDocRestSession.java @ 0:408254cf2f1d
Erstellung
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 24 Nov 2010 17:24:23 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:408254cf2f1d |
---|---|
1 package de.mpg.mpiwg.berlin.mpdl.escidoc; | |
2 | |
3 import java.io.BufferedReader; | |
4 import java.io.FileInputStream; | |
5 import java.io.IOException; | |
6 import java.io.InputStream; | |
7 import java.io.InputStreamReader; | |
8 import java.io.OutputStream; | |
9 import java.net.HttpURLConnection; | |
10 import java.net.URL; | |
11 import java.util.ArrayList; | |
12 import java.util.Date; | |
13 import java.util.Iterator; | |
14 import java.util.regex.Matcher; | |
15 import java.util.regex.Pattern; | |
16 | |
17 import javax.xml.namespace.NamespaceContext; | |
18 | |
19 import org.apache.commons.codec.binary.Base64; | |
20 import org.apache.commons.httpclient.Cookie; | |
21 import org.apache.commons.httpclient.Header; | |
22 import org.apache.commons.httpclient.HttpClient; | |
23 import org.apache.commons.httpclient.HttpException; | |
24 import org.apache.commons.httpclient.cookie.CookiePolicy; | |
25 import org.apache.commons.httpclient.cookie.CookieSpec; | |
26 import org.apache.commons.httpclient.methods.DeleteMethod; | |
27 import org.apache.commons.httpclient.methods.GetMethod; | |
28 import org.apache.commons.httpclient.methods.PostMethod; | |
29 import org.apache.commons.httpclient.methods.PutMethod; | |
30 import org.apache.commons.httpclient.params.HttpMethodParams; | |
31 | |
32 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException; | |
33 import de.mpg.mpiwg.berlin.mpdl.general.MpdlConstants; | |
34 import de.mpg.mpiwg.berlin.mpdl.util.FileUtil; | |
35 import de.mpg.mpiwg.berlin.mpdl.util.XmlUtil; | |
36 | |
37 public class ESciDocRestSession { | |
38 private static String CONTENT_ENCODING = "utf-8"; | |
39 private static String STAGE_PATH = "/st/staging-file"; | |
40 private static String LINE_SEPARATOR = System.getProperty("line.separator"); | |
41 private static Pattern PATTERN_XML_BASE_ATTRIBUTE = Pattern.compile("xml:base=\"([^\"]*)\""); | |
42 private static Pattern PATTERN_XLINK_HREF_ATTRIBUTE = Pattern.compile("xlink:href=\"([^\"]*)\""); | |
43 private String protocol = "http"; | |
44 private String host = "escidoc-dev.mpiwg-berlin.mpg.de"; | |
45 private int port = 8080; | |
46 private String contentModelId; | |
47 private String contextId; | |
48 private HttpClient httpClient; | |
49 private String cookieId; | |
50 | |
51 public static ESciDocRestSession getInstance(String cookieId) throws ApplicationException { | |
52 ESciDocRestSession instance = new ESciDocRestSession(); | |
53 instance.protocol = "http"; | |
54 instance.host = MpdlConstants.MPDL_ESCIDOC_HOST_NAME; | |
55 instance.port = MpdlConstants.MPDL_ESCIDOC_PORT; | |
56 instance.contentModelId = MpdlConstants.MPDL_ESCIDOC_CMM_ID; | |
57 instance.contextId = MpdlConstants.MPDL_ESCIDOC_CONTEXT_ID; | |
58 instance.cookieId = cookieId; | |
59 instance.httpClient = new HttpClient(); | |
60 return instance; | |
61 } | |
62 | |
63 public static String login(String userName, String pw) throws ApplicationException { | |
64 String protocol = "http"; | |
65 String host = MpdlConstants.MPDL_ESCIDOC_HOST_NAME; | |
66 int port = MpdlConstants.MPDL_ESCIDOC_PORT; | |
67 String cookieId = null; | |
68 try { | |
69 String frameworkUrl = protocol + "://" + host + ":" + port; | |
70 HttpClient client = new HttpClient(); | |
71 client.getHostConfiguration().setHost(host, port, "http"); | |
72 client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); | |
73 | |
74 PostMethod login = new PostMethod( frameworkUrl + "/aa/j_spring_security_check"); | |
75 login.addParameter("j_username", userName); | |
76 login.addParameter("j_password", pw); | |
77 client.executeMethod(login); | |
78 login.releaseConnection(); | |
79 | |
80 CookieSpec cookiespec = CookiePolicy.getDefaultSpec(); | |
81 Cookie[] logoncookies = cookiespec.match(host, port, "/", false, client.getState().getCookies()); | |
82 Cookie sessionCookie = logoncookies[0]; | |
83 | |
84 PostMethod postMethod = new PostMethod("/aa/login"); | |
85 postMethod.addParameter("target", frameworkUrl); | |
86 client.getState().addCookie(sessionCookie); | |
87 client.executeMethod(postMethod); | |
88 Header headers[] = postMethod.getResponseHeaders(); | |
89 for (int i = 0; i < headers.length; ++i) { | |
90 if ("Location".equals(headers[i].getName())) { | |
91 String location = headers[i].getValue(); | |
92 int index = location.indexOf('='); | |
93 Base64 base64 = new Base64(); | |
94 String locationTemp = location.substring(index + 1, location.length()); | |
95 cookieId = new String(base64.decode(locationTemp.getBytes())); | |
96 } | |
97 } | |
98 // if login is possible but the grants are not enough | |
99 if (cookieId != null) { | |
100 ESciDocRestSession session = getInstance(cookieId); | |
101 String grantAdminHref = session.getGrantHrefByUserNameAndRoleName(userName, "escidoc:role-system-administrator"); | |
102 if (grantAdminHref == null) | |
103 cookieId = "-10"; | |
104 } | |
105 postMethod.releaseConnection(); | |
106 } catch (Exception e) { | |
107 throw new ApplicationException(e); | |
108 } | |
109 return cookieId; | |
110 } | |
111 | |
112 // validation service of eSciDoc // TODO not implemented yet | |
113 public void validate(String pid, MetadataRecord mdRecord, String srcUrl) throws ApplicationException { | |
114 Component component = new Component("valid", "public", "any fulltext", "text/xml", srcUrl, "internal-managed"); | |
115 ArrayList<Component> components = new ArrayList<Component>(); | |
116 components.add(component); | |
117 Item xmlTemplate = new Item(contextId, pid, mdRecord, contentModelId, components); | |
118 String itemXmlStr = xmlTemplate.toXmlString(); | |
119 String uri = "/validation/rest/validateItemXmlBySchema"; | |
120 HttpMethodParams parameter = new HttpMethodParams(); | |
121 parameter.setParameter("validation-point", ""); // None (Pick the validation schema from the context provided with the item) | |
122 parameter.setParameter("validation-schema", ""); // None (Default) | |
123 String valAnswer = performPostRequest(uri, itemXmlStr, parameter); | |
124 } | |
125 | |
126 public String getCookieId() { | |
127 return cookieId; | |
128 } | |
129 | |
130 public void openContext(String contextId) throws ApplicationException { | |
131 String contextXmlStr = getContextById(contextId); | |
132 Date lastModificationDate = getLastModificationDate(contextXmlStr); | |
133 String lastModificationDateStr = XmlUtil.getInstance().toXsDate(lastModificationDate); | |
134 String bodyContentStr = "<param last-modification-date=\"" + lastModificationDateStr + "\"/>"; | |
135 String uri = "/ir/context/" + contextId + "/open"; | |
136 performPostRequestByBody(uri, bodyContentStr); | |
137 } | |
138 | |
139 public String createContext(String organizationalUnit, String name, String description, String type) throws ApplicationException { | |
140 Context xmlTemplate = new Context(organizationalUnit, name, description, type); | |
141 String bodyContentXmlStr = xmlTemplate.toXmlString(); | |
142 String contextXmlStr = performPutRequestByBody("/ir/context", bodyContentXmlStr); | |
143 String contextId = getFirstContextId(contextXmlStr); | |
144 return contextId; | |
145 } | |
146 | |
147 public String getContextById(String contextId) throws ApplicationException { | |
148 String bodyContent = "<param><filter><id>" + contextId + "</id></filter></param>"; | |
149 String requestUrlStr = "/ir/contexts/filter"; | |
150 String resultXmlStr = performPostRequestByBody(requestUrlStr, bodyContent); | |
151 return resultXmlStr; | |
152 } | |
153 | |
154 public void grant(String userName, String roleName) throws ApplicationException { | |
155 String grantXmlStr = null; | |
156 String userId = null; | |
157 String internalRoleName = null; | |
158 if (roleName != null && roleName.equals("admin")) { | |
159 internalRoleName = "escidoc:role-system-administrator"; | |
160 userId = getUserId(userName); | |
161 Grant grant = new Grant(userName, userId, "System-Administrator", "/aa/role/" + internalRoleName); | |
162 grantXmlStr = grant.toXmlString(); | |
163 } | |
164 String grantHref = getGrantHrefByUserNameAndRoleName(userName, internalRoleName); | |
165 if (grantHref == null || grantHref.equals("")) | |
166 performPutRequestByBody(userId + "/resources/grants/grant", grantXmlStr); | |
167 } | |
168 | |
169 public String getGrantHrefByUserNameAndRoleName(String userName, String roleName) throws ApplicationException { | |
170 String resultXmlStr = null; | |
171 String fullUserId = getUserId(userName); // // e.g. userId=/aa/user-account/escidoc:22650 | |
172 if (fullUserId != null) { | |
173 int userIdIndex = fullUserId.lastIndexOf("/"); | |
174 if (userIdIndex != -1) { | |
175 String userId = fullUserId.substring(userIdIndex + 1); | |
176 String filterUserName = "<filter name=\"http://escidoc.de/core/01/properties/user\">" + userId + "</filter>"; // e.g. userId=escidoc:22650 | |
177 String filterRoleName = "<filter name=\"http://escidoc.de/core/01/properties/role\">" + roleName + "</filter>"; // e.g. roleName=escidoc:role-system-administrator | |
178 String bodyContent = "<param>" + filterUserName + filterRoleName + "</param>"; | |
179 String requestUrlStr = "/aa/grants/filter"; | |
180 resultXmlStr = performPostRequestByBody(requestUrlStr, bodyContent); | |
181 resultXmlStr = getFirstGrantId(resultXmlStr); | |
182 } | |
183 } | |
184 return resultXmlStr; | |
185 } | |
186 | |
187 public String getGrantsByUserName(String userName) throws ApplicationException { | |
188 String resultXmlStr = null; | |
189 String fullUserId = getUserId(userName); // // e.g. userId=/aa/user-account/escidoc:22650 | |
190 if (fullUserId != null) { | |
191 int userIdIndex = fullUserId.lastIndexOf("/"); | |
192 if (userIdIndex != -1) { | |
193 String userId = fullUserId.substring(userIdIndex + 1); | |
194 String filterUserName = "<filter name=\"http://escidoc.de/core/01/properties/user\">" + userId + "</filter>"; // e.g. userId=escidoc:22650 | |
195 String bodyContent = "<param>" + filterUserName + "</param>"; | |
196 String requestUrlStr = "/aa/grants/filter"; | |
197 resultXmlStr = performPostRequestByBody(requestUrlStr, bodyContent); | |
198 } | |
199 } | |
200 return resultXmlStr; | |
201 } | |
202 | |
203 public String createContainer(String pid, MetadataRecord mdRecord) throws ApplicationException { | |
204 Container xmlTemplate = new Container(contentModelId, contextId, pid, mdRecord); | |
205 String bodyContentXmlStr = xmlTemplate.toXmlString(); | |
206 String containerXmlStr = performPutRequestByBody("/ir/container", bodyContentXmlStr); | |
207 String containerId = getFirstContainerId(containerXmlStr); | |
208 return containerId; | |
209 } | |
210 | |
211 public Container createContainerInContainer(String pid, MetadataRecord mdRecord, String containerId) throws ApplicationException { | |
212 Container xmlTemplate = new Container(contentModelId, contextId, pid, mdRecord); | |
213 String bodyContentXmlStr = xmlTemplate.toXmlString(); | |
214 String uri = containerId + "/create-container"; | |
215 String containerXmlStr = performPostRequestByBody(uri, bodyContentXmlStr); | |
216 String retContainerId = getFirstContainerId(containerXmlStr); | |
217 Date lastModificationDate = getLastModificationDate(containerXmlStr); | |
218 Container container = new Container(retContainerId, lastModificationDate); | |
219 return container; | |
220 } | |
221 | |
222 public Item createItemInContainer(String containerId, String pid, MetadataRecord mdRecord, ArrayList<Component> components) throws ApplicationException { | |
223 Item xmlTemplate = new Item(contextId, pid, mdRecord, contentModelId, components); | |
224 String bodyContentXmlStr = xmlTemplate.toXmlString(); | |
225 String uri = containerId + "/create-item"; | |
226 String itemXmlStr = performPostRequestByBody(uri, bodyContentXmlStr); | |
227 String itemId = getFirstItemId(itemXmlStr); | |
228 Date lastModificationDate = getLastModificationDate(itemXmlStr); | |
229 Item item = new Item(itemId, lastModificationDate); | |
230 return item; | |
231 } | |
232 | |
233 public Item createItemInContainer(String containerId, String itemXmlStr) throws ApplicationException { | |
234 String uri = containerId + "/create-item"; | |
235 String retItemXmlStr = performPostRequestByBody(uri, itemXmlStr); | |
236 String itemId = getFirstItemId(retItemXmlStr); | |
237 Date lastModificationDate = getLastModificationDate(retItemXmlStr); | |
238 String validStatus = ""; // TODO | |
239 String visibility = ""; // TODO | |
240 String contentCategory = ""; // TODO | |
241 String mimeType = ""; // TODO | |
242 String url = getFirstComponentId(retItemXmlStr); | |
243 String storage = ""; // TODO | |
244 Component component = new Component(validStatus, visibility, contentCategory, mimeType, url, storage); | |
245 Item item = new Item(itemId, lastModificationDate); | |
246 item.addComponent(component); | |
247 return item; | |
248 } | |
249 | |
250 public void submitContainer(String containerId, Date lastModificationDate, String comment) throws ApplicationException { | |
251 String uri = containerId + "/submit"; | |
252 String dateStr = XmlUtil.getInstance().toXsDate(lastModificationDate); | |
253 String xmlStr = "<param last-modification-date=\"" + dateStr + "\"><comment>" + comment + "</comment></param>"; | |
254 performPostRequestByBody(uri, xmlStr); | |
255 } | |
256 | |
257 public void submitItem(String itemId, Date lastModificationDate, String comment) throws ApplicationException { | |
258 String uri = itemId + "/submit"; | |
259 String dateStr = XmlUtil.getInstance().toXsDate(lastModificationDate); | |
260 String xmlStr = "<param last-modification-date=\"" + dateStr + "\"><comment>" + comment + "</comment></param>"; | |
261 performPostRequestByBody(uri, xmlStr); | |
262 } | |
263 | |
264 public Date addMembers(String containerId, Date lastModificationDate, ArrayList<String> memberIds) throws ApplicationException { | |
265 if (containerId == null || lastModificationDate == null || memberIds == null) | |
266 return null; | |
267 String dateStr = XmlUtil.getInstance().toXsDate(lastModificationDate); | |
268 String membersXmlStr = "<param last-modification-date=\"" + dateStr + "\">"; | |
269 for (int i=0; i< memberIds.size(); i++) { | |
270 String memberId = memberIds.get(i); | |
271 membersXmlStr = membersXmlStr + "<id>" + "escidoc:" + memberId +"</id>"; | |
272 } | |
273 membersXmlStr += "</param>"; | |
274 String lastModDateXmlStr = performPostRequestByBody(containerId + "/members/add", membersXmlStr); | |
275 Date lastModDate = getLastModificationDate(lastModDateXmlStr); | |
276 return lastModDate; | |
277 } | |
278 | |
279 public Date removeMembers(String containerId, Date lastModificationDate, ArrayList<String> memberIds) throws ApplicationException { | |
280 if (containerId == null || lastModificationDate == null || memberIds == null) | |
281 return null; | |
282 String dateStr = XmlUtil.getInstance().toXsDate(lastModificationDate); | |
283 String membersXmlStr = "<param last-modification-date=\"" + dateStr + "\">"; | |
284 for (int i=0; i< memberIds.size(); i++) { | |
285 String memberId = memberIds.get(i); | |
286 // if memberId is a full id and contains non digits they will be removed: e.g. /ir/item/escidoc:4711 will be replaced by 4711 | |
287 if (! memberId.matches("[0-9]+")) { | |
288 memberId = memberId.replaceAll("[^0-9]+", ""); | |
289 } | |
290 membersXmlStr = membersXmlStr + "<id>" + "escidoc:" + memberId +"</id>"; | |
291 } | |
292 membersXmlStr += "</param>"; | |
293 String lastModDateXmlStr = performPostRequestByBody(containerId + "/members/remove", membersXmlStr); | |
294 Date lastModDate = getLastModificationDate(lastModDateXmlStr); | |
295 return lastModDate; | |
296 } | |
297 | |
298 public Item createItem(String pid, MetadataRecord mdRecord, ArrayList<Component> components) throws ApplicationException { | |
299 Item xmlTemplate = new Item(contextId, pid, mdRecord, contentModelId, components); | |
300 String xmlStr = xmlTemplate.toXmlString(); | |
301 String itemXmlStr = performPutRequestByBody("/ir/item", xmlStr); | |
302 String itemId = getFirstItemId(itemXmlStr); | |
303 Date lastModificationDate = getLastModificationDate(itemXmlStr); | |
304 Item item = new Item(itemId, lastModificationDate); | |
305 return item; | |
306 } | |
307 | |
308 public Date updateItem(String itemId, Date lastModificationDate, String pid, MetadataRecord mdRecord, ArrayList<Component> components) throws ApplicationException { | |
309 if (itemId == null || lastModificationDate == null) | |
310 return null; | |
311 Item xmlTemplate = new Item(contextId, pid, mdRecord, contentModelId, components); | |
312 xmlTemplate.setLastModificationDate(lastModificationDate); | |
313 String xmlStr = xmlTemplate.toXmlString(); | |
314 String itemXmlStr = performPutRequestByBody(itemId, xmlStr); | |
315 Date newVersionDate = getVersionDate(itemXmlStr); | |
316 return newVersionDate; | |
317 } | |
318 | |
319 public void deleteItem(String itemId) { | |
320 if (itemId != null) { | |
321 performDeleteRequest(itemId); | |
322 } | |
323 } | |
324 | |
325 public void deleteContainer(String containerId) { | |
326 if (containerId != null) { | |
327 performDeleteRequest(containerId); | |
328 } | |
329 } | |
330 | |
331 public Date getContainerLastModificationDate(String containerId) throws ApplicationException { | |
332 Date lastModificationDate = null; | |
333 String resultXmlStr = getContainer(containerId); | |
334 if (resultXmlStr != null) { | |
335 lastModificationDate = getLastModificationDate(resultXmlStr); | |
336 } | |
337 return lastModificationDate; | |
338 } | |
339 | |
340 public String getContainer(String containerId) throws ApplicationException { | |
341 String resultXmlStr = null; | |
342 if (containerId != null) { | |
343 resultXmlStr = performGetRequest(containerId); | |
344 } | |
345 return resultXmlStr; | |
346 } | |
347 | |
348 public String getItem(String itemId) throws ApplicationException { | |
349 String resultXmlStr = null; | |
350 if (itemId != null) { | |
351 resultXmlStr = performGetRequest(itemId); | |
352 } | |
353 return resultXmlStr; | |
354 } | |
355 | |
356 public void saveComponentContentToLocalFile(String componentContentId, String localFileName) throws ApplicationException { | |
357 if (componentContentId != null) { | |
358 performGetRequestToLocalFile(componentContentId, localFileName); | |
359 } | |
360 } | |
361 | |
362 public String getUserId(String userName) throws ApplicationException { | |
363 String userId = null; | |
364 if (userName != null) { | |
365 String userNameAccessStr = userName + ",uid=" + userName + ",ou=users,dc=wisges,dc=rz-berlin,dc=mpg,dc=de"; | |
366 String resultXmlStr = performGetRequest("/aa/user-account/" + userNameAccessStr); | |
367 userId = getFirstUserId(resultXmlStr); | |
368 } | |
369 return userId; | |
370 } | |
371 | |
372 public String getUser(String userName) throws ApplicationException { | |
373 String resultXmlStr = null; | |
374 if (userName != null) { | |
375 String userNameAccessStr = userName + ",uid=" + userName + ",ou=users,dc=wisges,dc=rz-berlin,dc=mpg,dc=de"; | |
376 resultXmlStr = performGetRequest("/aa/user-account/" + userNameAccessStr); | |
377 } | |
378 return resultXmlStr; | |
379 } | |
380 | |
381 public String getAllUsers() throws ApplicationException { | |
382 String bodyContent = "<param><filter></filter></param>"; | |
383 String requestUrlStr = "/aa/user-accounts/filter"; | |
384 String resultXmlStr = performPostRequestByBody(requestUrlStr, bodyContent); | |
385 return resultXmlStr; | |
386 } | |
387 | |
388 public String getMembersByContainerIdAndFilter(String containerId, String filter) throws ApplicationException { | |
389 String bodyContent = "<param><filter></filter></param>"; | |
390 if (filter != null) | |
391 bodyContent = "<param>" + filter + "</param>"; | |
392 String requestUrlStr = containerId + "/members/filter"; | |
393 String resultXmlStr = performPostRequestByBody(requestUrlStr, bodyContent); | |
394 return resultXmlStr; | |
395 } | |
396 | |
397 public String getAllItems() throws ApplicationException { | |
398 String bodyContent = "<param><filter></filter></param>"; | |
399 String requestUrlStr = "/ir/items/filter"; | |
400 String resultXmlStr = performPostRequestByBody(requestUrlStr, bodyContent); | |
401 return resultXmlStr; | |
402 } | |
403 | |
404 private String getContainerByEXistId(String existId) throws ApplicationException { | |
405 String bodyContent = "<param><filter name=\"/md-records/md-record/metadata/exist-identifier\">" + existId + "</filter></param>"; // e.g. existId = /echo/la/alvarus_1509_lat_V40_10.xml | |
406 String requestUrlStr = "/ir/containers/filter"; | |
407 String resultXmlStr = performPostRequestByBody(requestUrlStr, bodyContent); | |
408 return resultXmlStr; | |
409 } | |
410 | |
411 public String getContainerIdByEXistId(String existId) throws ApplicationException { | |
412 String containerXmlStr = getContainerByEXistId(existId); | |
413 String eScidDocContainerId = null; | |
414 if (containerXmlStr != null && containerXmlStr != "") { | |
415 eScidDocContainerId = getFirstContainerId(containerXmlStr); | |
416 } | |
417 return eScidDocContainerId; | |
418 } | |
419 | |
420 public String getItemByPid(String pid) throws ApplicationException { | |
421 String bodyContent = "<param><filter name=\"/properties/pid\">" + pid + "</filter></param>"; | |
422 String requestUrlStr = "/ir/items/filter"; | |
423 String resultXmlStr = performPostRequestByBody(requestUrlStr, bodyContent); | |
424 return resultXmlStr; | |
425 } | |
426 | |
427 public String uploadFileToESciDocStageArea(String filePath) throws ApplicationException { | |
428 StringBuffer result = new StringBuffer(); | |
429 try { | |
430 URL createUrl = new URL(protocol + "://" + host + ":" + port + STAGE_PATH); | |
431 HttpURLConnection uploadConnection = (HttpURLConnection) createUrl.openConnection(); | |
432 uploadConnection.setRequestProperty("Cookie", "escidocCookie=" + cookieId); | |
433 uploadConnection.setRequestMethod("PUT"); | |
434 uploadConnection.setDoOutput(true); | |
435 // open POST Request | |
436 OutputStream out = uploadConnection.getOutputStream(); | |
437 // access binary content | |
438 InputStream in = new FileInputStream(filePath); | |
439 // write template to POST Request | |
440 byte[] bytes = new byte[4096]; | |
441 int l = in.read(bytes); | |
442 while (l > -1) { | |
443 out.write(bytes, 0, l); | |
444 l = in.read(bytes); | |
445 } | |
446 in.close(); | |
447 out.close(); | |
448 uploadConnection.connect(); | |
449 // connect response reader | |
450 BufferedReader createdReader = null; | |
451 String contentEncoding = uploadConnection.getContentEncoding(); | |
452 if (contentEncoding == null) { | |
453 contentEncoding = CONTENT_ENCODING; | |
454 } | |
455 createdReader = new BufferedReader(new InputStreamReader(uploadConnection.getInputStream(), contentEncoding)); | |
456 // read response | |
457 String line = createdReader.readLine(); | |
458 while (line != null) { | |
459 result.append(line); | |
460 result.append(LINE_SEPARATOR); | |
461 line = createdReader.readLine(); | |
462 } | |
463 createdReader.close(); | |
464 } catch (IOException e) { | |
465 throw new ApplicationException(e); | |
466 } | |
467 String stageUrl = obtainResourceHref(result.toString()); | |
468 return stageUrl; | |
469 } | |
470 | |
471 | |
472 public String getPid() throws ApplicationException { | |
473 return "mpiwg:47114711"; // TODO | |
474 /* | |
475 try { | |
476 XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl(); | |
477 XmlRpcClient client = new XmlRpcClient(); | |
478 String zopeUrlStr = "http://xserve07.mpiwg-berlin.mpg.de:18080"; | |
479 config.setServerURL(new URL(zopeUrlStr + "/idGenerator")); | |
480 client.setConfig(config); | |
481 Object[] params = new Object[]{}; | |
482 String pid = (String) client.execute("generateId", params); | |
483 return pid; | |
484 } catch (MalformedURLException e) { | |
485 throw new ApplicationException(e); | |
486 } catch (XmlRpcException e) { | |
487 throw new ApplicationException(e); | |
488 } | |
489 */ | |
490 } | |
491 | |
492 public ArrayList<String> getContainerIds(String xmlStr) throws ApplicationException { | |
493 NamespaceContext nsContext = getNsContext(); | |
494 XmlUtil xmlUtil = XmlUtil.getInstance(); | |
495 ArrayList<String> containerIds = xmlUtil.evaluateToStringArray(xmlStr, "//srel:container/@xlink:href", nsContext); | |
496 return containerIds; | |
497 } | |
498 | |
499 public ArrayList<String> getContainerTitles(String xmlStr) throws ApplicationException { | |
500 NamespaceContext nsContext = getNsContext(); | |
501 XmlUtil xmlUtil = XmlUtil.getInstance(); | |
502 ArrayList<String> containerTitles = xmlUtil.evaluateToStringArray(xmlStr, "//srel:container/@xlink:title", nsContext); | |
503 return containerTitles; | |
504 } | |
505 | |
506 public String getLatestVersionId(String xmlStr) throws ApplicationException { | |
507 NamespaceContext nsContext = getNsContext(); | |
508 XmlUtil xmlUtil = XmlUtil.getInstance(); | |
509 String id = xmlUtil.evaluateToString(xmlStr, "//prop:latest-version/@xlink:href", nsContext); | |
510 return id; | |
511 } | |
512 | |
513 public String getFirstUserId(String xmlStr) throws ApplicationException { | |
514 NamespaceContext nsContext = getNsContext(); | |
515 XmlUtil xmlUtil = XmlUtil.getInstance(); | |
516 String id = xmlUtil.evaluateToString(xmlStr, "//user-account:user-account/@xlink:href", nsContext); | |
517 return id; | |
518 } | |
519 | |
520 public String getFirstGrantId(String xmlStr) throws ApplicationException { | |
521 NamespaceContext nsContext = getNsContext(); | |
522 XmlUtil xmlUtil = XmlUtil.getInstance(); | |
523 String g = xmlUtil.evaluateToString(xmlStr, "//grants:grant/@xlink:href", nsContext); | |
524 return g; | |
525 } | |
526 | |
527 public String getFirstContextId(String xmlStr) throws ApplicationException { | |
528 NamespaceContext nsContext = getNsContext(); | |
529 XmlUtil xmlUtil = XmlUtil.getInstance(); | |
530 String id = xmlUtil.evaluateToString(xmlStr, "//context:context/@xlink:href", nsContext); | |
531 return id; | |
532 } | |
533 | |
534 public String getFirstContainerId(String xmlStr) throws ApplicationException { | |
535 NamespaceContext nsContext = getNsContext(); | |
536 XmlUtil xmlUtil = XmlUtil.getInstance(); | |
537 String id = xmlUtil.evaluateToString(xmlStr, "//container:container/@xlink:href", nsContext); | |
538 return id; | |
539 } | |
540 | |
541 public String getFirstItemId(String xmlStr) throws ApplicationException { | |
542 NamespaceContext nsContext = getNsContext(); | |
543 XmlUtil xmlUtil = XmlUtil.getInstance(); | |
544 String id = xmlUtil.evaluateToString(xmlStr, "//escidocItem:item/@xlink:href", nsContext); | |
545 return id; | |
546 } | |
547 | |
548 public String getFirstStageAreaURL(String xmlStr) throws ApplicationException { | |
549 NamespaceContext nsContext = getNsContext(); | |
550 XmlUtil xmlUtil = XmlUtil.getInstance(); | |
551 String eSciDocStageAreaUrl = xmlUtil.evaluateToString(xmlStr, "//escidocComponents:content/@xlink:href", nsContext); | |
552 return eSciDocStageAreaUrl; | |
553 } | |
554 | |
555 public String getFirstComponentId(String xmlStr) throws ApplicationException { | |
556 NamespaceContext nsContext = getNsContext(); | |
557 XmlUtil xmlUtil = XmlUtil.getInstance(); | |
558 String componentId = xmlUtil.evaluateToString(xmlStr, "//escidocComponents:content/@xlink:href", nsContext); | |
559 return componentId; | |
560 } | |
561 | |
562 public String getFirstEXistId(String xmlStr) throws ApplicationException { | |
563 NamespaceContext nsContext = getNsContext(); | |
564 XmlUtil xmlUtil = XmlUtil.getInstance(); | |
565 String id = xmlUtil.evaluateToString(xmlStr, "/escidocItem:item/escidocMetadataRecords:md-records/escidocMetadataRecords:md-record/metadata/mpiwg:exist-identifier", nsContext); | |
566 return id; | |
567 } | |
568 | |
569 public MetadataRecord getFirstMdRecord(String xmlStr) throws ApplicationException { | |
570 NamespaceContext nsContext = getNsContext(); | |
571 XmlUtil xmlUtil = XmlUtil.getInstance(); | |
572 String id = xmlUtil.evaluateToString(xmlStr, "/escidocItem:item/escidocMetadataRecords:md-records/escidocMetadataRecords:md-record/metadata/dc:identifier", nsContext); | |
573 String language = xmlUtil.evaluateToString(xmlStr, "/escidocItem:item/escidocMetadataRecords:md-records/escidocMetadataRecords:md-record/metadata/dc:language", nsContext); | |
574 String creator = xmlUtil.evaluateToString(xmlStr, "/escidocItem:item/escidocMetadataRecords:md-records/escidocMetadataRecords:md-record/metadata/dc:creator", nsContext); | |
575 String title = xmlUtil.evaluateToString(xmlStr, "/escidocItem:item/escidocMetadataRecords:md-records/escidocMetadataRecords:md-record/metadata/dc:title", nsContext); | |
576 String type = xmlUtil.evaluateToString(xmlStr, "/escidocItem:item/escidocMetadataRecords:md-records/escidocMetadataRecords:md-record/metadata/dc:type", nsContext); | |
577 String rights = xmlUtil.evaluateToString(xmlStr, "/escidocItem:item/escidocMetadataRecords:md-records/escidocMetadataRecords:md-record/metadata/dc:rights", nsContext); | |
578 String dateStr = xmlUtil.evaluateToString(xmlStr, "/escidocItem:item/escidocMetadataRecords:md-records/escidocMetadataRecords:md-record/metadata/dc:date", nsContext); | |
579 Date date = new Date(dateStr); | |
580 String license = xmlUtil.evaluateToString(xmlStr, "/escidocItem:item/escidocMetadataRecords:md-records/escidocMetadataRecords:md-record/metadata/mpiwg:license", nsContext); | |
581 String accessRights = xmlUtil.evaluateToString(xmlStr, "/escidocItem:item/escidocMetadataRecords:md-records/escidocMetadataRecords:md-record/metadata/mpiwg:accessRights", nsContext); | |
582 String mediaType = xmlUtil.evaluateToString(xmlStr, "/escidocItem:item/escidocMetadataRecords:md-records/escidocMetadataRecords:md-record/metadata/mpiwg:mediaType", nsContext); | |
583 String existId = xmlUtil.evaluateToString(xmlStr, "/escidocItem:item/escidocMetadataRecords:md-records/escidocMetadataRecords:md-record/metadata/mpiwg:exist-identifier", nsContext); | |
584 MetadataRecord mdRecord = new MetadataRecord(id, language, creator, title, null, null, type, rights, date); // TODO vervollständigen, testen | |
585 mdRecord.setLicense(license); | |
586 mdRecord.setAccessRights(accessRights); | |
587 mdRecord.setMediaType(mediaType); | |
588 mdRecord.setEXistIdentifier(existId); | |
589 return mdRecord; | |
590 } | |
591 | |
592 public Date getVersionDate(String xmlStr) throws ApplicationException { | |
593 NamespaceContext nsContext = getNsContext(); | |
594 XmlUtil xmlUtil = XmlUtil.getInstance(); | |
595 String dateStr = xmlUtil.evaluateToString(xmlStr, "//version:date", nsContext); | |
596 Date lastModificationDate = xmlUtil.toDate(dateStr); | |
597 return lastModificationDate; | |
598 } | |
599 | |
600 public Date getLastModificationDate(String xmlStr) throws ApplicationException { | |
601 NamespaceContext nsContext = getNsContext(); | |
602 XmlUtil xmlUtil = XmlUtil.getInstance(); | |
603 String dateStr = xmlUtil.evaluateToString(xmlStr, "//*/@last-modification-date", nsContext); | |
604 Date lastModificationDate = xmlUtil.toDate(dateStr); | |
605 return lastModificationDate; | |
606 } | |
607 | |
608 private String obtainResourceHref(String xml) { | |
609 // base | |
610 String base = ""; | |
611 Matcher baseMatcher = PATTERN_XML_BASE_ATTRIBUTE.matcher(xml); | |
612 if (baseMatcher.find()) { | |
613 base = baseMatcher.group(1); | |
614 } | |
615 // href | |
616 String href = null; | |
617 Matcher hrefMatcher = PATTERN_XLINK_HREF_ATTRIBUTE.matcher(xml); | |
618 if (hrefMatcher.find()) { | |
619 href = hrefMatcher.group(1); | |
620 } else { | |
621 throw new UnsupportedOperationException("Can not obtain href for resources without xlink:href attribute."); | |
622 } | |
623 return base + href; | |
624 } | |
625 | |
626 private String performPostRequestByBody(String requestUrlStr, String bodyContent) throws ApplicationException { | |
627 String resultStr = null; | |
628 try { | |
629 String urlStr = protocol + "://" + host + ":" + port + requestUrlStr; | |
630 PostMethod method = new PostMethod(urlStr); | |
631 method.setFollowRedirects(false); | |
632 method.setRequestHeader("Cookie", "escidocCookie=" + cookieId); | |
633 if (bodyContent != null) { | |
634 method.setRequestBody(bodyContent); | |
635 } | |
636 httpClient.executeMethod(method); | |
637 byte[] responseBody = method.getResponseBody(); | |
638 resultStr = new String(responseBody, "utf-8"); | |
639 method.releaseConnection(); | |
640 } catch (HttpException e) { | |
641 throw new ApplicationException(e); | |
642 } catch (IOException e) { | |
643 throw new ApplicationException(e); | |
644 } | |
645 return resultStr; | |
646 } | |
647 | |
648 // TODO | |
649 private String performPostRequest(String requestUrlStr, String bodyContent, HttpMethodParams parameter) throws ApplicationException { | |
650 String resultStr = null; | |
651 try { | |
652 String urlStr = protocol + "://" + host + ":" + port + requestUrlStr; | |
653 PostMethod method = new PostMethod(urlStr); | |
654 method.setFollowRedirects(false); | |
655 method.setRequestHeader("Cookie", "escidocCookie=" + cookieId); | |
656 if (bodyContent != null) { | |
657 method.setRequestBody(bodyContent); | |
658 } | |
659 if (parameter != null) { | |
660 method.setParams(parameter); | |
661 } | |
662 httpClient.executeMethod(method); | |
663 byte[] responseBody = method.getResponseBody(); | |
664 resultStr = new String(responseBody, "utf-8"); | |
665 method.releaseConnection(); | |
666 } catch (HttpException e) { | |
667 throw new ApplicationException(e); | |
668 } catch (IOException e) { | |
669 throw new ApplicationException(e); | |
670 } | |
671 return resultStr; | |
672 } | |
673 | |
674 private String performPutRequestByBody(String requestName, String bodyContent) { | |
675 String resultStr = null; | |
676 try { | |
677 String urlStr = protocol + "://" + host + ":" + port + requestName; | |
678 PutMethod method = new PutMethod(urlStr); | |
679 method.setRequestHeader("Cookie", "escidocCookie=" + cookieId); | |
680 if (bodyContent != null) { | |
681 method.setRequestBody(bodyContent); | |
682 } | |
683 httpClient.executeMethod(method); | |
684 byte[] responseBody = method.getResponseBody(); | |
685 resultStr = new String(responseBody, "utf-8"); | |
686 method.releaseConnection(); | |
687 } catch (HttpException e) { | |
688 e.printStackTrace(); | |
689 } catch (IOException e) { | |
690 e.printStackTrace(); | |
691 } | |
692 return resultStr; | |
693 } | |
694 | |
695 private void performDeleteRequest(String requestName) { | |
696 try { | |
697 String urlStr = protocol + "://" + host + ":" + port + requestName; | |
698 DeleteMethod method = new DeleteMethod(urlStr); | |
699 method.setRequestHeader("Cookie", "escidocCookie=" + cookieId); | |
700 httpClient.executeMethod(method); | |
701 method.releaseConnection(); | |
702 } catch (HttpException e) { | |
703 e.printStackTrace(); | |
704 } catch (IOException e) { | |
705 e.printStackTrace(); | |
706 } | |
707 } | |
708 | |
709 private String performGetRequest(String requestName) { | |
710 String resultStr = null; | |
711 try { | |
712 String urlStr = protocol + "://" + host + ":" + port + requestName; | |
713 GetMethod method = new GetMethod(urlStr); | |
714 method.setRequestHeader("Cookie", "escidocCookie=" + cookieId); | |
715 httpClient.executeMethod(method); | |
716 byte[] responseBody = method.getResponseBody(); | |
717 resultStr = new String(responseBody, "utf-8"); | |
718 method.releaseConnection(); | |
719 } catch (HttpException e) { | |
720 e.printStackTrace(); | |
721 } catch (IOException e) { | |
722 e.printStackTrace(); | |
723 } | |
724 return resultStr; | |
725 } | |
726 | |
727 private void performGetRequestToLocalFile(String requestName, String localFileName) throws ApplicationException { | |
728 try { | |
729 String urlStr = protocol + "://" + host + ":" + port + requestName; | |
730 GetMethod method = new GetMethod(urlStr); | |
731 method.setRequestHeader("Cookie", "escidocCookie=" + cookieId); | |
732 httpClient.executeMethod(method); | |
733 InputStream responseBodyInputStream = method.getResponseBodyAsStream(); | |
734 FileUtil.getInstance().saveInputStreamToLocalFile(responseBodyInputStream, localFileName); | |
735 method.releaseConnection(); | |
736 } catch (HttpException e) { | |
737 throw new ApplicationException(e); | |
738 } catch (IOException e) { | |
739 throw new ApplicationException(e); | |
740 } | |
741 } | |
742 | |
743 public static NamespaceContext getNsContext() { | |
744 NamespaceContext nsContext = new NamespaceContext() { | |
745 public String getNamespaceURI(String prefix) { | |
746 String uri; | |
747 if (prefix.equals("xlink")) | |
748 uri = "http://www.w3.org/1999/xlink"; | |
749 else if (prefix.equals("escidocItem")) | |
750 uri = "http://www.escidoc.de/schemas/item/0.9"; | |
751 else if (prefix.equals("user-account")) | |
752 uri = "http://www.escidoc.de/schemas/useraccount/0.7"; | |
753 else if (prefix.equals("grants")) | |
754 uri = "http://www.escidoc.de/schemas/grants/0.5"; | |
755 else if (prefix.equals("context")) | |
756 uri = "http://www.escidoc.de/schemas/context/0.7"; | |
757 else if (prefix.equals("container")) | |
758 uri = "http://www.escidoc.de/schemas/container/0.8"; | |
759 else if (prefix.equals("escidocMetadataRecords")) | |
760 uri = "http://www.escidoc.de/schemas/metadatarecords/0.5"; | |
761 else if (prefix.equals("escidocComponents")) | |
762 uri = "http://www.escidoc.de/schemas/components/0.9"; | |
763 else if (prefix.equals("prop")) | |
764 uri = "http://escidoc.de/core/01/properties"; | |
765 else if (prefix.equals("struct-map")) | |
766 uri = "http://www.escidoc.de/schemas/structmap/0.4"; | |
767 else if (prefix.equals("version")) | |
768 uri = "http://escidoc.de/core/01/properties/version/"; | |
769 else if (prefix.equals("srel")) | |
770 uri = "http://escidoc.de/core/01/structural-relations/"; | |
771 else if (prefix.equals("xml")) | |
772 uri = "http://www.w3.org/XML/1998/namespace"; | |
773 else if (prefix.equals("dc")) | |
774 uri = "http://purl.org/dc/elements/1.1/"; | |
775 else if (prefix.equals("mpiwg")) | |
776 uri = "http://www.mpiwg-berlin.mpg.de/ns/mpiwg"; | |
777 else | |
778 uri = null; | |
779 return uri; | |
780 } | |
781 | |
782 public String getPrefix(String uri) { | |
783 if (uri.equals("http://www.w3.org/1999/xlink")) | |
784 return "xlink"; | |
785 else if (uri.equals("http://www.escidoc.de/schemas/item/0.9")) | |
786 return "escidocItem"; | |
787 else if (uri.equals("http://www.escidoc.de/schemas/useraccount/0.7")) | |
788 return "user-account"; | |
789 else if (uri.equals("http://www.escidoc.de/schemas/grants/0.5")) | |
790 return "grants"; | |
791 else if (uri.equals("http://www.escidoc.de/schemas/context/0.7")) | |
792 return "context"; | |
793 else if (uri.equals("http://www.escidoc.de/schemas/container/0.8")) | |
794 return "container"; | |
795 else if (uri.equals("http://www.escidoc.de/schemas/metadatarecords/0.5")) | |
796 return "escidocMetadataRecords"; | |
797 else if (uri.equals("http://www.escidoc.de/schemas/components/0.9")) | |
798 return "escidocComponents"; | |
799 else if (uri.equals("http://escidoc.de/core/01/properties")) | |
800 return "prop"; | |
801 else if (uri.equals("http://www.escidoc.de/schemas/structmap/0.4")) | |
802 return "struct-map"; | |
803 else if (uri.equals("http://escidoc.de/core/01/properties/version/")) | |
804 return "version"; | |
805 else if (uri.equals("http://escidoc.de/core/01/structural-relations/")) | |
806 return "srel"; | |
807 else if (uri.equals("http://www.w3.org/XML/1998/namespace")) | |
808 return "xml"; | |
809 else if (uri.equals("http://purl.org/dc/elements/1.1/")) | |
810 return "dc"; | |
811 else if (uri.equals("http://www.mpiwg-berlin.mpg.de/ns/mpiwg")) | |
812 return "mpiwg"; | |
813 else | |
814 return null; | |
815 } | |
816 | |
817 public Iterator getPrefixes(String namespace) { | |
818 return null; | |
819 } | |
820 }; | |
821 return nsContext; | |
822 } | |
823 } |