1 package org.collectionspace.services.client;
4 import java.util.ArrayList;
8 import javax.ws.rs.core.MediaType;
9 import javax.ws.rs.core.MultivaluedMap;
10 import javax.ws.rs.core.Response;
12 import org.apache.commons.io.FileUtils;
13 import org.collectionspace.services.client.test.ServiceRequestType;
14 import org.collectionspace.services.common.api.Tools;
15 import org.collectionspace.services.concept.ConceptTermGroup;
16 import org.collectionspace.services.concept.ConceptauthoritiesCommon;
17 import org.dom4j.DocumentException;
18 import org.jboss.resteasy.client.ClientResponse;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
22 public class ConceptAuthorityClientUtils {
23 private static final Logger logger =
24 LoggerFactory.getLogger(ConceptAuthorityClientUtils.class);
27 * Creates a new Concept Authority
28 * @param displayName The displayName used in UI, etc.
29 * @param refName The proper refName for this authority
30 * @param headerLabel The common part label
31 * @return The PoxPayloadOut payload for the create call
33 public static PoxPayloadOut createConceptAuthorityInstance(
34 String displayName, String shortIdentifier, String headerLabel ) {
35 ConceptauthoritiesCommon conceptAuthority = new ConceptauthoritiesCommon();
36 conceptAuthority.setDisplayName(displayName);
37 conceptAuthority.setShortIdentifier(shortIdentifier);
38 conceptAuthority.setVocabType("ConceptAuthority"); //FIXME: REM - Should this really be hard-coded?
39 PoxPayloadOut multipart = new PoxPayloadOut(ConceptAuthorityClient.SERVICE_PAYLOAD_NAME);
40 PayloadOutputPart commonPart = multipart.addPart(conceptAuthority, MediaType.APPLICATION_XML_TYPE);
41 commonPart.setLabel(headerLabel);
43 if(logger.isDebugEnabled()){
44 logger.debug("to be created, conceptAuthority common ",
45 conceptAuthority, ConceptauthoritiesCommon.class);
52 * @param commonPartXML the XML payload for the common part.
53 * @param headerLabel The common part label
54 * @return The PoxPayloadOut payload for the create call
55 * @throws DocumentException
57 public static PoxPayloadOut createConceptInstance(
58 String commonPartXML, String headerLabel) throws DocumentException {
59 PoxPayloadOut multipart = new PoxPayloadOut(ConceptAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
61 PayloadOutputPart commonPart = multipart.addPart(commonPartXML,
62 MediaType.APPLICATION_XML_TYPE);
63 commonPart.setLabel(headerLabel);
65 PayloadOutputPart commonPart = multipart.addPart(
66 ConceptAuthorityClient.SERVICE_ITEM_COMMON_PART_NAME,
69 if(logger.isDebugEnabled()){
70 logger.debug("to be created, concept common ", commonPart.asXML());
76 public static String createItemInAuthority(String vcsid,
78 ConceptAuthorityClient client ) throws DocumentException {
79 // Expected status code: 201 Created
80 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
81 // Type of service request being tested
82 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
84 PoxPayloadOut multipart =
85 createConceptInstance(commonPartXML, client.getItemCommonPartName());
87 Response res = client.createItem(vcsid, multipart);
89 int statusCode = res.getStatus();
91 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
92 throw new RuntimeException("Could not create Item: \""+commonPartXML
93 +"\" in conceptAuthority: \"" + vcsid
94 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
96 if(statusCode != EXPECTED_STATUS_CODE) {
97 throw new RuntimeException("Unexpected Status when creating Item: \""+commonPartXML
98 +"\" in conceptAuthority: \"" + vcsid +"\", Status:"+ statusCode);
100 newID = extractId(res);
109 * Creates the from xml file.
111 * @param fileName the file name
112 * @return new CSID as string
113 * @throws Exception the exception
115 private String createItemInAuthorityFromXmlFile(String vcsid, String commonPartFileName,
116 ConceptAuthorityClient client) throws Exception {
117 byte[] b = FileUtils.readFileToByteArray(new File(commonPartFileName));
118 String commonPartXML = new String(b);
119 return createItemInAuthority(vcsid, commonPartXML, client );
122 public static String extractId(Response res) {
123 MultivaluedMap<String, Object> mvm = res.getMetadata();
124 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
125 if(logger.isDebugEnabled()){
126 logger.debug("extractId:uri=" + uri);
128 String[] segments = uri.split("/");
129 String id = segments[segments.length - 1];
130 if(logger.isDebugEnabled()){
131 logger.debug("id=" + id);
137 * Returns an error message indicating that the status code returned by a
138 * specific call to a service does not fall within a set of valid status
139 * codes for that service.
141 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
143 * @param statusCode The invalid status code that was returned in the response,
144 * from submitting that type of request to the service.
146 * @return An error message.
148 public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
149 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
150 requestType.validStatusCodesAsString();
153 public static List<ConceptTermGroup> getTermGroupInstance(String identifier) {
154 if (Tools.isBlank(identifier)) {
155 identifier = getGeneratedIdentifier();
157 List<ConceptTermGroup> terms = new ArrayList<ConceptTermGroup>();
158 ConceptTermGroup term = new ConceptTermGroup();
159 term.setTermDisplayName(identifier);
160 term.setTermName(identifier);
165 private static String getGeneratedIdentifier() {
166 return "id" + new Date().getTime();