]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f817b2db5fb3b173b22f9225b19967d81858c2ec
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.client;
2
3 import java.io.File;
4 import java.util.ArrayList;
5
6 import javax.ws.rs.core.MediaType;
7 import javax.ws.rs.core.MultivaluedMap;
8 import javax.ws.rs.core.Response;
9
10 import org.apache.commons.io.FileUtils;
11 import org.collectionspace.services.client.test.ServiceRequestType;
12 import org.collectionspace.services.concept.ConceptauthoritiesCommon;
13 import org.dom4j.DocumentException;
14 import org.jboss.resteasy.client.ClientResponse;
15 import org.slf4j.Logger;
16 import org.slf4j.LoggerFactory;
17
18 public class ConceptAuthorityClientUtils {
19     private static final Logger logger =
20         LoggerFactory.getLogger(ConceptAuthorityClientUtils.class);
21
22     /**
23      * Creates a new Concept Authority
24      * @param displayName       The displayName used in UI, etc.
25      * @param refName           The proper refName for this authority
26      * @param headerLabel       The common part label
27      * @return  The PoxPayloadOut payload for the create call
28      */
29     public static PoxPayloadOut createConceptAuthorityInstance(
30                 String displayName, String shortIdentifier, String headerLabel ) {
31         ConceptauthoritiesCommon conceptAuthority = new ConceptauthoritiesCommon();
32         conceptAuthority.setDisplayName(displayName);
33         conceptAuthority.setShortIdentifier(shortIdentifier);
34         conceptAuthority.setVocabType("ConceptAuthority"); //FIXME: REM - Should this really be hard-coded?
35         PoxPayloadOut multipart = new PoxPayloadOut(ConceptAuthorityClient.SERVICE_PAYLOAD_NAME);
36         PayloadOutputPart commonPart = multipart.addPart(conceptAuthority, MediaType.APPLICATION_XML_TYPE);
37         commonPart.setLabel(headerLabel);
38
39         if(logger.isDebugEnabled()){
40                 logger.debug("to be created, conceptAuthority common ", 
41                                         conceptAuthority, ConceptauthoritiesCommon.class);
42         }
43
44         return multipart;
45     }
46
47     /**
48      * @param commonPartXML the XML payload for the common part.
49      * @param headerLabel       The common part label
50      * @return  The PoxPayloadOut payload for the create call
51      * @throws DocumentException
52      */
53     public static PoxPayloadOut createConceptInstance(
54                 String commonPartXML, String headerLabel)  throws DocumentException {
55         PoxPayloadOut multipart = new PoxPayloadOut(ConceptAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
56         /*
57         PayloadOutputPart commonPart = multipart.addPart(commonPartXML,
58             MediaType.APPLICATION_XML_TYPE);
59         commonPart.setLabel(headerLabel);
60         */
61         PayloadOutputPart commonPart = multipart.addPart(
62                         ConceptAuthorityClient.SERVICE_ITEM_COMMON_PART_NAME,
63                         commonPartXML);
64
65         if(logger.isDebugEnabled()){
66                 logger.debug("to be created, concept common ", commonPart.asXML());
67         }
68
69         return multipart;
70     }
71     
72     public static String createItemInAuthority(String vcsid,
73                 String commonPartXML,
74                 ConceptAuthorityClient client ) throws DocumentException {
75         // Expected status code: 201 Created
76         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
77         // Type of service request being tested
78         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
79         
80         PoxPayloadOut multipart = 
81                 createConceptInstance(commonPartXML, client.getItemCommonPartName());
82         String newID = null;
83         ClientResponse<Response> res = client.createItem(vcsid, multipart);
84         try {
85                 int statusCode = res.getStatus();
86         
87                 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
88                         throw new RuntimeException("Could not create Item: \""+commonPartXML
89                                         +"\" in conceptAuthority: \"" + vcsid
90                                         +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
91                 }
92                 if(statusCode != EXPECTED_STATUS_CODE) {
93                         throw new RuntimeException("Unexpected Status when creating Item: \""+commonPartXML
94                                         +"\" in conceptAuthority: \"" + vcsid +"\", Status:"+ statusCode);
95                 }
96                 newID = extractId(res);
97         } finally {
98                 res.releaseConnection();
99         }
100
101         return newID;
102     }
103     
104     /**
105      * Creates the from xml file.
106      *
107      * @param fileName the file name
108      * @return new CSID as string
109      * @throws Exception the exception
110      */
111     private String createItemInAuthorityFromXmlFile(String vcsid, String commonPartFileName, 
112                 ConceptAuthorityClient client) throws Exception {
113         byte[] b = FileUtils.readFileToByteArray(new File(commonPartFileName));
114         String commonPartXML = new String(b);
115         return createItemInAuthority(vcsid, commonPartXML, client );
116     }    
117
118     public static String extractId(ClientResponse<Response> res) {
119         MultivaluedMap<String, Object> mvm = res.getMetadata();
120         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
121         if(logger.isDebugEnabled()){
122                 logger.debug("extractId:uri=" + uri);
123         }
124         String[] segments = uri.split("/");
125         String id = segments[segments.length - 1];
126         if(logger.isDebugEnabled()){
127                 logger.debug("id=" + id);
128         }
129         return id;
130     }
131     
132     /**
133      * Returns an error message indicating that the status code returned by a
134      * specific call to a service does not fall within a set of valid status
135      * codes for that service.
136      *
137      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).
138      *
139      * @param statusCode  The invalid status code that was returned in the response,
140      *                    from submitting that type of request to the service.
141      *
142      * @return An error message.
143      */
144     public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
145         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
146                 requestType.validStatusCodesAsString();
147     }
148
149 }