]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
8b1d0cc74f80196f316e682f328499751b967bde
[tmp/jakarta-migration.git] /
1 /**
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:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright (c)) 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
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.
22  */
23
24 package org.collectionspace.services.collectionobject.client.sample;
25
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
29
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.MultivaluedMap;
32 import javax.ws.rs.core.Response;
33
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;
44
45 /**
46  * Sample, sample client code for creating and accessing 
47  * CollectionObject records.
48  *
49  * $LastChangedRevision: $
50  * $LastChangedDate: $
51  */
52 public class Sample {
53     private static final Logger logger =
54         LoggerFactory.getLogger(Sample.class);
55
56     // Instance variables specific to this test.
57     private CollectionObjectClient client = new CollectionObjectClient();
58     final String SERVICE_PATH_COMPONENT = "collectionobjects";
59
60
61     // ---------------------------------------------------------------
62     // Create
63     // ---------------------------------------------------------------
64
65
66     // ---------------------------------------------------------------
67     // Read
68     // ---------------------------------------------------------------
69
70     // ---------------------------------------------------------------
71     // Delete
72     // ---------------------------------------------------------------
73
74     // ---------------------------------------------------------------
75     // Utility methods
76     // ---------------------------------------------------------------
77
78     private Object extractPart(MultipartInput input, String label,
79         Class clazz) throws Exception {
80         Object obj = null;
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);
87                 }
88                 obj = part.getBody(clazz, null);
89                 if(logger.isDebugEnabled()){
90                     logger.debug("extracted part obj=\n", obj, clazz);
91                 }
92                 break;
93             }
94         }
95         return obj;
96     }
97
98     /**
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.
102      *
103      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).
104      *
105      * @param statusCode  The invalid status code that was returned in the response,
106      *                    from submitting that type of request to the service.
107      *
108      * @return An error message.
109      */
110     protected String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
111         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
112                 requestType.validStatusCodesAsString();
113     }
114
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);
120         }
121         String[] segments = uri.split("/");
122         String id = segments[segments.length - 1];
123         if(logger.isDebugEnabled()){
124                 logger.debug("id=" + id);
125         }
126         return id;
127     }
128     
129     public static void main(String[] args) {
130                 
131     }
132
133 }