]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b86578775bab28cd5f772aef201282fac3f4286b
[tmp/jakarta-migration.git] /
1 /**\r
2  * This document is a part of the source code and related artifacts\r
3  * for CollectionSpace, an open source collections management system\r
4  * for museums and related institutions:\r
5  *\r
6  * http://www.collectionspace.org\r
7  * http://wiki.collectionspace.org\r
8  *\r
9  * Copyright (c) 2009 Regents of the University of California\r
10  *\r
11  * Licensed under the Educational Community License (ECL), Version 2.0.\r
12  * You may not use this file except in compliance with this License.\r
13  *\r
14  * You may obtain a copy of the ECL 2.0 License at\r
15  * https://source.collectionspace.org/collection-space/LICENSE.txt\r
16  *\r
17  *  Unless required by applicable law or agreed to in writing, software\r
18  *  distributed under the License is distributed on an "AS IS" BASIS,\r
19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
20  *  See the License for the specific language governing permissions and\r
21  *  limitations under the License.\r
22  */\r
23 \r
24 package org.collectionspace.services.IntegrationTests.xmlreplay;\r
25 \r
26 import org.apache.commons.httpclient.HttpClient;\r
27 import org.apache.commons.httpclient.methods.DeleteMethod;\r
28 import org.apache.commons.httpclient.methods.GetMethod;\r
29 import org.apache.commons.io.FileUtils;\r
30 \r
31 import java.io.BufferedReader;\r
32 import java.io.File;\r
33 import java.io.InputStreamReader;\r
34 import java.io.OutputStreamWriter;\r
35 import java.net.HttpURLConnection;\r
36 import java.net.URL;\r
37 import java.util.List;\r
38 import java.util.Map;\r
39 \r
40 import org.collectionspace.services.IntegrationTests.xmlreplay.ServiceResult;\r
41 \r
42 /**\r
43  *   @author Laramie Crocker\r
44  */\r
45 public class XmlReplayTransport {\r
46 \r
47     private static String BOUNDARY = "34d97c83-0d61-4958-80ab-6bf8d362290f";\r
48         private static String DD = "--";\r
49         private static String CRLF = "\r\n";\r
50 \r
51     public static ServiceResult doGET(String urlString, String authForTest, String fromTestID) throws Exception {\r
52         HttpClient client = new HttpClient();\r
53         GetMethod getMethod = new GetMethod(urlString);\r
54         getMethod.addRequestHeader("Accept", "multipart/mixed");\r
55         getMethod.addRequestHeader("Accept", "application/xml");\r
56         getMethod.setRequestHeader("Authorization", "Basic " + authForTest); //"dGVzdDp0ZXN0");\r
57         getMethod.setRequestHeader("X-XmlReplay-fromTestID", fromTestID);\r
58         ServiceResult pr = new ServiceResult();\r
59 \r
60         int statusCode1 = client.executeMethod(getMethod);\r
61         pr.responseCode = statusCode1;\r
62         pr.method = "GET";\r
63         try {\r
64             pr.result = getMethod.getResponseBodyAsString();\r
65             pr.responseMessage = getMethod.getStatusText();\r
66         } catch (Throwable t){\r
67             //System.err.println("ERROR getting content from response: "+t);\r
68             pr.error = t.toString();\r
69         }\r
70 \r
71 \r
72         getMethod.releaseConnection();\r
73         return pr;\r
74     }\r
75 \r
76     public static ServiceResult doDELETE(String urlString, String authForTest, String testID, String fromTestID) throws Exception {\r
77         ServiceResult pr = new ServiceResult();\r
78         pr.method = "DELETE";\r
79         pr.fullURL = urlString;\r
80         if (Tools.isEmpty(urlString)){\r
81             pr.error = "url was empty.  Check the result for fromTestID: "+fromTestID+". currentTest: "+testID;\r
82             return pr;\r
83         }\r
84         HttpClient client = new HttpClient();\r
85         DeleteMethod deleteMethod = new DeleteMethod(urlString);\r
86         deleteMethod.setRequestHeader("Accept", "multipart/mixed");\r
87         deleteMethod.addRequestHeader("Accept", "application/xml");\r
88         deleteMethod.setRequestHeader("Authorization", "Basic " + authForTest);\r
89         deleteMethod.setRequestHeader("X-XmlReplay-fromTestID", fromTestID);\r
90         int statusCode1 = 0;\r
91         String res = "";\r
92         try {\r
93             statusCode1 = client.executeMethod(deleteMethod);\r
94             pr.responseCode = statusCode1;\r
95             //System.out.println("statusCode: "+statusCode1+" statusLine ==>" + deleteMethod.getStatusLine());\r
96             pr.responseMessage = deleteMethod.getStatusText();\r
97             res = deleteMethod.getResponseBodyAsString();\r
98             deleteMethod.releaseConnection();\r
99         } catch (Throwable t){\r
100             pr.error = t.toString();\r
101         }\r
102         pr.result = res;\r
103         pr.responseCode = statusCode1;\r
104         return pr;\r
105     }\r
106 \r
107     public static ServiceResult doLIST(String urlString, String listQueryParams, String authForTest, String fromTestID) throws Exception {\r
108         //String u = Tools.glue(urlString, "/", "items/");\r
109         if (Tools.notEmpty(listQueryParams)){\r
110             urlString = Tools.glue(urlString, "?", listQueryParams);\r
111         }\r
112         return doGET(urlString, authForTest, fromTestID);\r
113     }\r
114 \r
115     public static final String MULTIPART_MIXED = "multipart/mixed";\r
116     public static final String APPLICATION_XML = "application/xml";\r
117 \r
118     /** Use this overload for multipart messages. */\r
119     public static ServiceResult doPOST_PUTFromXML_Multipart(List<String> filesList,\r
120                                                                       List<String> partsList,\r
121                                                                       String protoHostPort,\r
122                                                                       String uri,\r
123                                                                       String method,\r
124                                                                       XmlReplayEval evalStruct,\r
125                                                                       String authForTest,\r
126                                                                       String fromTestID)\r
127                                                                       throws Exception {\r
128         if (  filesList==null||filesList.size()==0\r
129             ||partsList==null||partsList.size()==0\r
130             ||(partsList.size() != filesList.size())){\r
131             throw new Exception("filesList and partsList must not be empty and must have the same number of items each.");\r
132         }\r
133         String content = DD + BOUNDARY;\r
134 \r
135         for (int i=0; i<partsList.size(); i++){\r
136             String fileName = filesList.get(i);\r
137             String commonPartName = partsList.get(i);\r
138             byte[] b = FileUtils.readFileToByteArray(new File(fileName));\r
139             String xmlString = new String(b);\r
140 \r
141             xmlString = evalStruct.eval(xmlString, evalStruct.serviceResultsMap, evalStruct.jexl, evalStruct.jc);\r
142 \r
143             content = content + CRLF + "label: "+commonPartName + CRLF\r
144                               + "Content-Type: application/xml" + CRLF\r
145                               + CRLF\r
146                               + xmlString + CRLF\r
147                               + DD + BOUNDARY;\r
148         }\r
149         content = content + DD;\r
150         String urlString = protoHostPort+uri;\r
151         return doPOST_PUT(urlString, content, BOUNDARY, method, MULTIPART_MIXED, authForTest, fromTestID); //method is POST or PUT.\r
152     }\r
153 \r
154     /** Use this overload for NON-multipart messages, that is, regular POSTs. */\r
155         public static ServiceResult doPOST_PUTFromXML(String fileName,\r
156                                                                 String protoHostPort,\r
157                                                                 String uri,\r
158                                                                 String method,\r
159                                                                 String contentType,\r
160                                                                 XmlReplayEval evalStruct,\r
161                                                                 String authForTest,\r
162                                                                 String fromTestID)\r
163     throws Exception {\r
164         byte[] b = FileUtils.readFileToByteArray(new File(fileName));\r
165         String xmlString = new String(b);\r
166         xmlString = evalStruct.eval(xmlString, evalStruct.serviceResultsMap, evalStruct.jexl, evalStruct.jc);\r
167         String urlString = protoHostPort+uri;\r
168         return doPOST_PUT(urlString, xmlString, BOUNDARY, method, contentType, authForTest, fromTestID); //method is POST or PUT.\r
169     }\r
170 \r
171 \r
172     public static ServiceResult doPOST_PUT(String urlString, String content, String boundary, String method, String contentType,\r
173                                            String authForTest, String fromTestID) throws Exception {\r
174         URL url = new URL(urlString);\r
175         HttpURLConnection conn;\r
176         conn = (HttpURLConnection) url.openConnection();\r
177 \r
178         if (MULTIPART_MIXED.equalsIgnoreCase(contentType)){\r
179             conn.setRequestProperty("Accept", "multipart/mixed");\r
180             conn.setRequestProperty("content-type", "multipart/mixed; boundary=" + boundary);\r
181         } else {\r
182             conn.setRequestProperty("Accept", "application/xml");\r
183             conn.setRequestProperty("content-type", contentType);\r
184         }\r
185         conn.setRequestProperty("Authorization", "Basic " + authForTest);  //TODO: remove test user : hard-coded as "dGVzdDp0ZXN0"\r
186         conn.setRequestProperty("Connection", "close");\r
187         conn.setRequestProperty("X-XmlReplay-fromTestID", fromTestID);\r
188         conn.setDoOutput(true);\r
189         conn.setDoInput(true);\r
190         conn.setRequestMethod(method); // "POST" or "PUT"\r
191         OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());\r
192         wr.write(content);\r
193         wr.flush();\r
194 \r
195         ServiceResult result = new ServiceResult();\r
196         try {\r
197             result.responseCode = conn.getResponseCode();\r
198             //System.out.println("responseCode: "+result.responseCode);\r
199             if (400 <= result.responseCode && result.responseCode <= 499){\r
200                 return result;\r
201             }\r
202             BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));\r
203             String line;\r
204             StringBuffer sb = new StringBuffer();\r
205             while ((line = rd.readLine()) != null) {\r
206                 sb.append(line).append("\r\n");\r
207             }\r
208             String msg = sb.toString();\r
209             result.result = msg;\r
210             rd.close();\r
211         } catch (Throwable t){\r
212             //System.err.println("ERROR getting content from response: "+t);\r
213             result.error = t.toString();\r
214         }\r
215         wr.close();\r
216 \r
217 \r
218         String deleteURL = "";\r
219         String location = "";\r
220         Map<String, List<String>> headers = conn.getHeaderFields();\r
221         List<String> locations = headers.get("Location");\r
222         if (locations != null){\r
223             String locationZero = locations.get(0);\r
224             if (locationZero != null){\r
225                 String[] segments = locationZero.split("/");\r
226                 location = segments[segments.length - 1];\r
227                 deleteURL = Tools.glue(urlString, "/", location);\r
228             }\r
229         }\r
230         result.location = location;\r
231         result.deleteURL = deleteURL;\r
232         result.CSID = location;\r
233         result.method = method;\r
234         return result;\r
235     }\r
236 \r
237 }\r