2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright (c)) 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
24 package org.collectionspace.services.collectionobject.client.sample;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.MultivaluedMap;
32 import javax.ws.rs.core.Response;
34 import org.apache.log4j.BasicConfigurator;
35 import org.collectionspace.services.client.CollectionObjectClient;
36 import org.collectionspace.services.client.test.ServiceRequestType;
37 import org.jboss.resteasy.client.ClientResponse;
38 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
39 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
40 import org.jboss.resteasy.plugins.providers.multipart.InputPart;
41 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * Sample, sample client code for creating and accessing
47 * CollectionObject records.
49 * $LastChangedRevision: $
53 private static final Logger logger =
54 LoggerFactory.getLogger(Sample.class);
56 // Instance variables specific to this test.
57 private CollectionObjectClient client = new CollectionObjectClient();
58 final String SERVICE_PATH_COMPONENT = "collectionobjects";
61 // ---------------------------------------------------------------
63 // ---------------------------------------------------------------
66 // ---------------------------------------------------------------
68 // ---------------------------------------------------------------
70 // ---------------------------------------------------------------
72 // ---------------------------------------------------------------
74 // ---------------------------------------------------------------
76 // ---------------------------------------------------------------
78 private Object extractPart(MultipartInput input, String label,
79 Class clazz) throws Exception {
81 for(InputPart part : input.getParts()){
82 String partLabel = part.getHeaders().getFirst("label");
83 if(label.equalsIgnoreCase(partLabel)){
84 String partStr = part.getBodyAsString();
85 if(logger.isDebugEnabled()){
86 logger.debug("extracted part str=\n" + partStr);
88 obj = part.getBody(clazz, null);
89 if(logger.isDebugEnabled()){
90 logger.debug("extracted part obj=\n", obj, clazz);
99 * Returns an error message indicating that the status code returned by a
100 * specific call to a service does not fall within a set of valid status
101 * codes for that service.
103 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
105 * @param statusCode The invalid status code that was returned in the response,
106 * from submitting that type of request to the service.
108 * @return An error message.
110 protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
111 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
112 requestType.validStatusCodesAsString();
115 protected String extractId(ClientResponse<Response> res) {
116 MultivaluedMap<String, Object> mvm = res.getMetadata();
117 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
118 if(logger.isDebugEnabled()){
119 logger.info("extractId:uri=" + uri);
121 String[] segments = uri.split("/");
122 String id = segments[segments.length - 1];
123 if(logger.isDebugEnabled()){
124 logger.debug("id=" + id);
129 public static void main(String[] args) {