]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
CSPACE-5271: Added initial draft of UriTemplate class. Renamed UriBuilder to UriTempl...
authorAron Roberts <aron@socrates.berkeley.edu>
Thu, 7 Jun 2012 20:12:23 +0000 (13:12 -0700)
committerAron Roberts <aron@socrates.berkeley.edu>
Thu, 7 Jun 2012 20:12:23 +0000 (13:12 -0700)
services/common/src/main/java/org/collectionspace/services/common/UriBuilder.java [deleted file]
services/common/src/main/java/org/collectionspace/services/common/UriTemplate.java [new file with mode: 0644]
services/common/src/main/java/org/collectionspace/services/common/UriTemplateBuilder.java [new file with mode: 0644]

diff --git a/services/common/src/main/java/org/collectionspace/services/common/UriBuilder.java b/services/common/src/main/java/org/collectionspace/services/common/UriBuilder.java
deleted file mode 100644 (file)
index 237cd45..0000000
+++ /dev/null
@@ -1,65 +0,0 @@
-/**\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 org.jboss.resteasy.spi.touri;\r
-\r
-public class UriBuilder {\r
-\r
-    UriBuilderType uriBuilderType = null;\r
-\r
-    public UriBuilder(UriBuilderType type) {\r
-        this.uriBuilderType = type;\r
-    }\r
-\r
-    public UriBuilderType getType() {\r
-        return this.uriBuilderType;\r
-    }\r
-\r
-    @Override\r
-    public String toString() {\r
-        return "URI Builder of type " + getType().toString();\r
-    }\r
-    // Placeholder\r
-    String uriTemplate = "replace with true URITemplate object";\r
-\r
-    public String getURITemplate() {\r
-        switch (uriBuilderType) {\r
-            case RESOURCE:\r
-                return uriTemplate;\r
-\r
-            case ITEM:\r
-                return uriTemplate;\r
-\r
-            case CONTACT:\r
-                return uriTemplate;\r
-\r
-            default:\r
-                return uriTemplate;\r
-        }\r
-    }\r
-\r
-    public enum UriBuilderType {\r
-        RESOURCE, ITEM, CONTACT\r
-    };\r
-}
\ No newline at end of file
diff --git a/services/common/src/main/java/org/collectionspace/services/common/UriTemplate.java b/services/common/src/main/java/org/collectionspace/services/common/UriTemplate.java
new file mode 100644 (file)
index 0000000..a47f38d
--- /dev/null
@@ -0,0 +1,74 @@
+/**\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.net.URI;\r
+import java.util.Map;\r
+import javax.ws.rs.core.UriBuilder;\r
+import org.collectionspace.services.common.api.Tools;\r
+\r
+public class UriTemplate {\r
+\r
+    UriBuilder builder;\r
+    String path;\r
+\r
+    public UriTemplate(String path) {\r
+        setUriPath(path);\r
+        setBuilder();\r
+    }\r
+\r
+    private void setBuilder() {\r
+        if (builder == null) {\r
+            try {\r
+                builder = UriBuilder.fromPath(path);\r
+            } catch (IllegalArgumentException iae) {\r
+                // FIXME: Need to add logger and log error\r
+                // Will silently fail to initialize builder if relative URI is null\r
+                // No other checking of path format is apparently done\r
+            }\r
+        }\r
+    }\r
+\r
+    private void setUriPath(String path) {\r
+        if (Tools.notBlank(this.path)) {\r
+            this.path = this.path;\r
+        }\r
+    }\r
+\r
+    private UriBuilder getBuilder() {\r
+        if (builder == null) {\r
+            setBuilder();\r
+        }\r
+        return builder;\r
+    }\r
+\r
+    public String buildUri(Map<String, String> varsMap) {\r
+        URI uri = getBuilder().buildFromMap(varsMap);\r
+        if (uri != null) {\r
+            return uri.toString();\r
+        } else {\r
+            return "";\r
+        }\r
+    }\r
+\r
+}
\ No newline at end of file
diff --git a/services/common/src/main/java/org/collectionspace/services/common/UriTemplateBuilder.java b/services/common/src/main/java/org/collectionspace/services/common/UriTemplateBuilder.java
new file mode 100644 (file)
index 0000000..c91f93d
--- /dev/null
@@ -0,0 +1,76 @@
+/**\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
+public class UriTemplateBuilder {\r
+\r
+    UriTemplateType uriTemplateType = null;\r
+    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
+    final static String ITEM_TEMPLATE_PATTERN =\r
+            "/{servicename}/{identifier}/items/{itemIdentifier}";\r
+    final static String CONTACT_TEMPLATE_PATTERN =\r
+            "/{servicename}/{identifier}/items/{itemIdentifier}/contacts/{contactIdentifier}";\r
+    final static UriTemplate RESOURCE_URI_TEMPLATE =\r
+            new UriTemplate(RESOURCE_TEMPLATE_PATTERN);\r
+    final static UriTemplate ITEM_URI_TEMPLATE =\r
+            new UriTemplate(ITEM_TEMPLATE_PATTERN);\r
+    final static UriTemplate CONTACT_URI_TEMPLATE =\r
+            new UriTemplate(CONTACT_TEMPLATE_PATTERN);\r
+\r
+    public UriTemplateBuilder(UriTemplateType type) {\r
+        this.uriTemplateType = type;\r
+    }\r
+\r
+    public UriTemplateType getType() {\r
+        return this.uriTemplateType;\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        return "URI Builder of type " + getType().toString();\r
+    }\r
+\r
+    public UriTemplate getURITemplate() {\r
+        switch (uriTemplateType) {\r
+            case RESOURCE:\r
+                return RESOURCE_URI_TEMPLATE;\r
+\r
+            case ITEM:\r
+                return ITEM_URI_TEMPLATE;\r
+\r
+            case CONTACT:\r
+                return CONTACT_URI_TEMPLATE;\r
+\r
+            default:\r
+                return RESOURCE_URI_TEMPLATE;\r
+        }\r
+    }\r
+\r
+    public enum UriTemplateType {\r
+\r
+        RESOURCE, ITEM, CONTACT\r
+    };\r
+}
\ No newline at end of file