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
6 * http://www.collectionspace.org
\r
7 * http://wiki.collectionspace.org
\r
9 * Copyright (c) 2009 Regents of the University of California
\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
14 * You may obtain a copy of the ECL 2.0 License at
\r
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
\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
24 package org.collectionspace.services.IntegrationTests.xmlreplay;
\r
26 import org.apache.commons.httpclient.Header;
\r
27 import org.apache.commons.httpclient.HttpClient;
\r
28 import org.apache.commons.httpclient.methods.DeleteMethod;
\r
29 import org.apache.commons.httpclient.methods.GetMethod;
\r
30 import org.apache.commons.io.FileUtils;
\r
32 import java.io.BufferedReader;
\r
33 import java.io.File;
\r
34 import java.io.InputStreamReader;
\r
35 import java.io.OutputStreamWriter;
\r
36 import java.net.HttpURLConnection;
\r
37 import java.net.URL;
\r
38 import java.util.Arrays;
\r
39 import java.util.List;
\r
40 import java.util.Map;
\r
42 import org.collectionspace.services.common.Tools;
\r
45 * @author Laramie Crocker
\r
47 public class XmlReplayTransport {
\r
49 private static String BOUNDARY = "34d97c83-0d61-4958-80ab-6bf8d362290f";
\r
50 private static String DD = "--";
\r
51 private static String CRLF = "\r\n";
\r
53 public static ServiceResult doGET(String urlString, String authForTest, String fromTestID) throws Exception {
\r
54 HttpClient client = new HttpClient();
\r
55 GetMethod getMethod = new GetMethod(urlString);
\r
56 getMethod.addRequestHeader("Accept", "multipart/mixed");
\r
57 getMethod.addRequestHeader("Accept", "application/xml");
\r
58 getMethod.setRequestHeader("Authorization", "Basic " + authForTest); //"dGVzdDp0ZXN0");
\r
59 getMethod.setRequestHeader("X-XmlReplay-fromTestID", fromTestID);
\r
60 ServiceResult pr = new ServiceResult();
\r
62 pr.fromTestID = fromTestID;
\r
65 int statusCode1 = client.executeMethod(getMethod);
\r
66 pr.responseCode = statusCode1;
\r
67 pr.result = getMethod.getResponseBodyAsString();
\r
68 pr.responseMessage = getMethod.getStatusText();
\r
69 Header[] headers = getMethod.getResponseHeaders();
\r
70 pr.responseHeaders = Arrays.copyOf(headers, headers.length);
\r
71 Header hdr = getMethod.getResponseHeader("CONTENT-TYPE");
\r
73 String hdrStr = hdr.toExternalForm();
\r
74 pr.boundary = PayloadLogger.parseBoundary(hdrStr);
\r
76 pr.contentLength = getMethod.getResponseContentLength();
\r
77 getMethod.releaseConnection();
\r
78 } catch (Throwable t){
\r
79 //System.err.println("ERROR getting content from response: "+t);
\r
80 pr.error = t.toString();
\r
87 public static ServiceResult doDELETE(String urlString, String authForTest, String testID, String fromTestID) throws Exception {
\r
88 ServiceResult pr = new ServiceResult();
\r
89 pr.method = "DELETE";
\r
90 pr.fullURL = urlString;
\r
91 pr.fromTestID = fromTestID;
\r
92 if (Tools.isEmpty(urlString)){
\r
93 pr.error = "url was empty. Check the result for fromTestID: "+fromTestID+". currentTest: "+testID;
\r
96 HttpClient client = new HttpClient();
\r
97 DeleteMethod deleteMethod = new DeleteMethod(urlString);
\r
98 deleteMethod.setRequestHeader("Accept", "multipart/mixed");
\r
99 deleteMethod.addRequestHeader("Accept", "application/xml");
\r
100 deleteMethod.setRequestHeader("Authorization", "Basic " + authForTest);
\r
101 deleteMethod.setRequestHeader("X-XmlReplay-fromTestID", fromTestID);
\r
102 int statusCode1 = 0;
\r
105 statusCode1 = client.executeMethod(deleteMethod);
\r
106 pr.responseCode = statusCode1;
\r
107 //System.out.println("statusCode: "+statusCode1+" statusLine ==>" + deleteMethod.getStatusLine());
\r
108 pr.responseMessage = deleteMethod.getStatusText();
\r
109 res = deleteMethod.getResponseBodyAsString();
\r
110 deleteMethod.releaseConnection();
\r
111 } catch (Throwable t){
\r
112 pr.error = t.toString();
\r
115 pr.responseCode = statusCode1;
\r
119 public static ServiceResult doLIST(String urlString, String listQueryParams, String authForTest, String fromTestID) throws Exception {
\r
120 //String u = Tools.glue(urlString, "/", "items/");
\r
121 if (Tools.notEmpty(listQueryParams)){
\r
122 urlString = Tools.glue(urlString, "?", listQueryParams);
\r
124 return doGET(urlString, authForTest, fromTestID);
\r
127 public static final String MULTIPART_MIXED = "multipart/mixed";
\r
128 public static final String APPLICATION_XML = "application/xml";
\r
130 /** Use this overload for multipart messages. */
\r
131 public static ServiceResult doPOST_PUTFromXML_Multipart(List<String> filesList,
\r
132 List<String> partsList,
\r
133 String protoHostPort,
\r
136 XmlReplayEval evalStruct,
\r
137 String authForTest,
\r
140 if ( filesList==null||filesList.size()==0
\r
141 ||partsList==null||partsList.size()==0
\r
142 ||(partsList.size() != filesList.size())){
\r
143 throw new Exception("filesList and partsList must not be empty and must have the same number of items each.");
\r
145 String content = DD + BOUNDARY;
\r
147 for (int i=0; i<partsList.size(); i++){
\r
148 String fileName = filesList.get(i);
\r
149 String commonPartName = partsList.get(i);
\r
150 byte[] b = FileUtils.readFileToByteArray(new File(fileName));
\r
151 String xmlString = new String(b);
\r
153 xmlString = evalStruct.eval(xmlString, evalStruct.serviceResultsMap, evalStruct.jexl, evalStruct.jc);
\r
155 content = content + CRLF + "label: "+commonPartName + CRLF
\r
156 + "Content-Type: application/xml" + CRLF
\r
161 content = content + DD;
\r
162 String urlString = protoHostPort+uri;
\r
163 return doPOST_PUT(urlString, content, BOUNDARY, method, MULTIPART_MIXED, authForTest, fromTestID); //method is POST or PUT.
\r
166 /** Use this overload for NON-multipart messages, that is, regular POSTs. */
\r
167 public static ServiceResult doPOST_PUTFromXML(String fileName,
\r
168 String protoHostPort,
\r
171 String contentType,
\r
172 XmlReplayEval evalStruct,
\r
173 String authForTest,
\r
176 byte[] b = FileUtils.readFileToByteArray(new File(fileName));
\r
177 String xmlString = new String(b);
\r
178 xmlString = evalStruct.eval(xmlString, evalStruct.serviceResultsMap, evalStruct.jexl, evalStruct.jc);
\r
179 String urlString = protoHostPort+uri;
\r
180 return doPOST_PUT(urlString, xmlString, BOUNDARY, method, contentType, authForTest, fromTestID); //method is POST or PUT.
\r
184 public static ServiceResult doPOST_PUT(String urlString, String content, String boundary, String method, String contentType,
\r
185 String authForTest, String fromTestID) throws Exception {
\r
186 ServiceResult result = new ServiceResult();
\r
187 result.method = method;
\r
189 URL url = new URL(urlString);
\r
190 HttpURLConnection conn;
\r
191 conn = (HttpURLConnection) url.openConnection();
\r
193 if (MULTIPART_MIXED.equalsIgnoreCase(contentType)){
\r
194 conn.setRequestProperty("Accept", "multipart/mixed");
\r
195 conn.setRequestProperty("content-type", "multipart/mixed; boundary=" + boundary);
\r
197 conn.setRequestProperty("Accept", "application/xml");
\r
198 conn.setRequestProperty("content-type", contentType);
\r
200 conn.setRequestProperty("Authorization", "Basic " + authForTest); //TODO: remove test user : hard-coded as "dGVzdDp0ZXN0"
\r
201 conn.setRequestProperty("Connection", "close");
\r
202 conn.setRequestProperty("X-XmlReplay-fromTestID", fromTestID);
\r
203 conn.setDoOutput(true);
\r
204 conn.setDoInput(true);
\r
205 conn.setRequestMethod(method); // "POST" or "PUT"
\r
206 OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
\r
212 result.requestPayload = content;
\r
213 result.responseCode = conn.getResponseCode();
\r
214 //System.out.println("responseCode: "+result.responseCode);
\r
215 if (400 <= result.responseCode && result.responseCode <= 499){
\r
218 BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
\r
220 StringBuffer sb = new StringBuffer();
\r
221 while ((line = rd.readLine()) != null) {
\r
222 sb.append(line).append("\r\n");
\r
224 String msg = sb.toString();
\r
225 result.result = msg;
\r
226 result.boundary = PayloadLogger.parseBoundary(conn.getHeaderField("CONTENT-TYPE"));
\r
229 } catch (Throwable t){
\r
230 //System.err.println("ERROR getting content from response: "+t);
\r
231 result.error = t.toString();
\r
236 String deleteURL = "";
\r
237 String location = "";
\r
238 Map<String, List<String>> headers = conn.getHeaderFields();
\r
239 List<String> locations = headers.get("Location");
\r
240 if (locations != null){
\r
241 String locationZero = locations.get(0);
\r
242 if (locationZero != null){
\r
243 String[] segments = locationZero.split("/");
\r
244 location = segments[segments.length - 1];
\r
245 deleteURL = Tools.glue(urlString, "/", location);
\r
248 result.location = location;
\r
249 result.deleteURL = deleteURL;
\r
250 result.CSID = location;
\r
252 } catch (Throwable t2){
\r
253 result.error = "ERROR in XmlReplayTransport: "+t2;
\r