import org.collectionspace.services.common.ResourceMap;
import org.collectionspace.services.common.ResourceMapHolder;
import org.collectionspace.services.common.ResourceMapImpl;
+import org.collectionspace.services.common.UriTemplateRegistry;
import org.collectionspace.services.common.security.SecurityInterceptor;
import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
private Set<Object> singletons = new HashSet<Object>();
private Set<Class<?>> empty = new HashSet<Class<?>>();
private ResourceMap resourceMap = new ResourceMapImpl();
+ private UriTemplateRegistry uriTemplateRegistry = new UriTemplateRegistry();
private ServletContext servletContext = null;
public CollectionSpaceJaxRsApplication() {
private void addResourceToMapAndSingletons(ResourceBase resource) {
singletons.add(resource);
resourceMap.put(resource.getServiceName(), resource);
+ // uriTemplateRegistry.put(resource.getDocType(), resource.getUriTemplate());
}
@Override
--- /dev/null
+/**
+ * This document is a part of the source code and related artifacts for
+ * CollectionSpace, an open source collections management system for museums and
+ * related institutions:
+ *
+ * http://www.collectionspace.org http://wiki.collectionspace.org
+ *
+ * Copyright © 2009-2012 University of California, Berkeley
+ *
+ * Licensed under the Educational Community License (ECL), Version 2.0. You may
+ * not use this file except in compliance with this License.
+ *
+ * You may obtain a copy of the ECL 2.0 License at
+ *
+ * https://source.collectionspace.org/collection-space/LICENSE.txt
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package org.collectionspace.services.common;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * StoredValuesUriTemplate.java
+ *
+ * Generates URI strings by combining a URI template with provided values, which
+ * replace variables within the template.
+ *
+ * In this subclass, some of the values which will replace variables in a URI
+ * template are static, and can be stored alongside the template for reuse.
+ * Additional values that will replace variables within the template are also
+ * dynamically accepted, and are merged with stored values when building URIs.
+ */
+public class StoredValuesUriTemplate extends UriTemplate {
+
+ private Map<String, String> storedValuesMap = new HashMap<String, String>();
+
+ public StoredValuesUriTemplate(String path, Map<String, String> storedValuesMap) {
+ super(path);
+ setStoredValuesMap(storedValuesMap);
+ }
+
+ private void setStoredValuesMap(Map<String, String> storedValuesMap) {
+ if (storedValuesMap != null && !storedValuesMap.isEmpty()) {
+ this.storedValuesMap = storedValuesMap;
+ }
+ }
+
+ private Map<String, String> getStoredValuesMap() {
+ return this.storedValuesMap;
+ }
+
+ /**
+ * Builds a URI string from a combination of previously-stored values (such
+ * as static URI path components) and additional values (such as resource
+ * identifiers), both of which will replace variables within the URI
+ * template.
+ *
+ * @param varsMap a map of values that will replace variables within the URI
+ * template
+ * @return a URI string
+ */
+ public String buildUri(Map<String, String> additionalValuesMap) {
+ Map<String, String> allValuesMap = new HashMap<String, String>();
+ allValuesMap.putAll(getStoredValuesMap());
+ if (storedValuesMap != null && !storedValuesMap.isEmpty()) {
+ allValuesMap.putAll(additionalValuesMap);
+ }
+ return super.buildUri(allValuesMap);
+ }
+}
*\r
* http://www.collectionspace.org http://wiki.collectionspace.org\r
*\r
- * Copyright 2009-2012 University of California, Berkeley\r
+ * Copyright © 2009-2012 University of California, Berkeley\r
*\r
* Licensed under the Educational Community License (ECL), Version 2.0. You may\r
* not use this file except in compliance with this License.\r
import org.slf4j.Logger;\r
import org.slf4j.LoggerFactory;\r
\r
+/**\r
+ * UriTemplate.java\r
+ *\r
+ * Generates URI strings by combining a URI template with provided values, which\r
+ * replace variables within the template.\r
+ */\r
public class UriTemplate {\r
\r
private static final Logger logger = LoggerFactory.getLogger(UriTemplate.class);\r
return getUriPath();\r
}\r
\r
- public String buildUri(Map<String, String> varsMap) {\r
+ public String buildUri(Map<String, String> valuesMap) {\r
URI uri = null;\r
try {\r
- uri = getBuilder().buildFromMap(varsMap);\r
+ uri = getBuilder().buildFromMap(valuesMap);\r
} catch (IllegalArgumentException iae) {\r
logger.warn("One or more required parameter values were missing "\r
+ "when building URI value via URI Template: " + iae.getMessage());\r
*\r
* http://www.collectionspace.org http://wiki.collectionspace.org\r
*\r
- * Copyright 2009-2012 University of California, Berkeley\r
+ * Copyright © 2009-2012 University of California, Berkeley\r
*\r
* Licensed under the Educational Community License (ECL), Version 2.0. You may\r
* not use this file except in compliance with this License.\r
*/\r
package org.collectionspace.services.common;\r
\r
+/**\r
+ * UriTemplateFactory.java\r
+ *\r
+ * A factory for building instances of URITemplate classes, based on a provided\r
+ * template type.\r
+ */\r
public class UriTemplateFactory {\r
\r
public final static String RESOURCE_TEMPLATE_PATTERN =\r
"/{servicename}/{identifier}";\r
- // FIXME: Get static strings below (e.g. "items") from already-declared\r
- // constants elsewhere\r
+ // FIXME: Get static strings below (e.g. "items", "contacts") from\r
+ // already-declared constants elsewhere\r
public final static String ITEM_TEMPLATE_PATTERN =\r
"/{servicename}/{identifier}/items/{itemIdentifier}";\r
public final static String CONTACT_TEMPLATE_PATTERN =\r
--- /dev/null
+/**\r
+ * This document is a part of the source code and related artifacts for\r
+ * CollectionSpace, an open source collections management system for museums and\r
+ * related institutions:\r
+ *\r
+ * http://www.collectionspace.org http://wiki.collectionspace.org\r
+ *\r
+ * Copyright © 2009-2012 University of California, Berkeley\r
+ *\r
+ * Licensed under the Educational Community License (ECL), Version 2.0. You may\r
+ * not use this file except in compliance with this License.\r
+ *\r
+ * You may obtain a copy of the ECL 2.0 License at\r
+ *\r
+ * https://source.collectionspace.org/collection-space/LICENSE.txt\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\r
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\r
+ * License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ */\r
+package org.collectionspace.services.common;\r
+\r
+import java.util.HashMap;\r
+\r
+/**\r
+ * UriTemplateRegistry.java\r
+ *\r
+ * Maps document types to templates for building URIs, used in turn for\r
+ * accessing instances of those documents.\r
+ */\r
+public class UriTemplateRegistry extends HashMap<String, StoredValuesUriTemplate> {\r
+}\r
public class UriTemplateTest {\r
\r
final static String EXAMPLE_SERVICE_NAME = "examples";\r
- final static String CSID = "a87f6616-4146-4c17-a41a-048597cc12aa";\r
+ final static String EXAMPLE_CSID = "a87f6616-4146-4c17-a41a-048597cc12aa";\r
private static final Logger logger = LoggerFactory.getLogger(UriTemplateTest.class);\r
\r
private void testBanner(String msg) {\r
UriTemplate resourceTemplate = UriTemplateFactory.getURITemplate(UriTemplateFactory.UriTemplateType.RESOURCE);\r
Map<String, String> resourceUriVars = new HashMap<String, String>();\r
resourceUriVars.put("servicename", EXAMPLE_SERVICE_NAME);\r
- resourceUriVars.put("identifier", CSID);\r
+ resourceUriVars.put("identifier", EXAMPLE_CSID);\r
String uriStr = resourceTemplate.buildUri(resourceUriVars);\r
Assert.assertFalse(Tools.isBlank(uriStr), "Generated URI string is null or blank.");\r
logger.debug("Generated URI string = " + uriStr);\r
}\r
\r
- @Test(dependsOnMethods = {"createResourceUriTemplate"})\r
+ @Test(dependsOnMethods = {"buildResourceUri"})\r
public void buildResourceUriWithMissingValue() {\r
testBanner("buildResourceUriWithMissingValue");\r
UriTemplate resourceTemplate = UriTemplateFactory.getURITemplate(UriTemplateFactory.UriTemplateType.RESOURCE);\r
logger.debug("Generated URI string = " + uriStr);\r
}\r
\r
- @Test(dependsOnMethods = {"createResourceUriTemplate"})\r
+ @Test(dependsOnMethods = {"buildResourceUri"})\r
public void buildResourceUriWithNullValue() {\r
testBanner("buildResourceUriWithNullValue");\r
UriTemplate resourceTemplate = UriTemplateFactory.getURITemplate(UriTemplateFactory.UriTemplateType.RESOURCE);\r