From: Aron Roberts Date: Thu, 7 Jun 2012 20:12:23 +0000 (-0700) Subject: CSPACE-5271: Added initial draft of UriTemplate class. Renamed UriBuilder to UriTempl... X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;h=738acf376825c11b480e5787c4e174445e3afbc3;p=tmp%2Fjakarta-migration.git CSPACE-5271: Added initial draft of UriTemplate class. Renamed UriBuilder to UriTemplateBuilder, to better describe its purpose and to avoid potential conflict with same-named RESTEasy class. --- 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 index 237cd451b..000000000 --- a/services/common/src/main/java/org/collectionspace/services/common/UriBuilder.java +++ /dev/null @@ -1,65 +0,0 @@ -/** - * 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 org.jboss.resteasy.spi.touri; - -public class UriBuilder { - - UriBuilderType uriBuilderType = null; - - public UriBuilder(UriBuilderType type) { - this.uriBuilderType = type; - } - - public UriBuilderType getType() { - return this.uriBuilderType; - } - - @Override - public String toString() { - return "URI Builder of type " + getType().toString(); - } - // Placeholder - String uriTemplate = "replace with true URITemplate object"; - - public String getURITemplate() { - switch (uriBuilderType) { - case RESOURCE: - return uriTemplate; - - case ITEM: - return uriTemplate; - - case CONTACT: - return uriTemplate; - - default: - return uriTemplate; - } - } - - public enum UriBuilderType { - RESOURCE, ITEM, CONTACT - }; -} \ 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 index 000000000..a47f38db2 --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/UriTemplate.java @@ -0,0 +1,74 @@ +/** + * 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.net.URI; +import java.util.Map; +import javax.ws.rs.core.UriBuilder; +import org.collectionspace.services.common.api.Tools; + +public class UriTemplate { + + UriBuilder builder; + String path; + + public UriTemplate(String path) { + setUriPath(path); + setBuilder(); + } + + private void setBuilder() { + if (builder == null) { + try { + builder = UriBuilder.fromPath(path); + } catch (IllegalArgumentException iae) { + // FIXME: Need to add logger and log error + // Will silently fail to initialize builder if relative URI is null + // No other checking of path format is apparently done + } + } + } + + private void setUriPath(String path) { + if (Tools.notBlank(this.path)) { + this.path = this.path; + } + } + + private UriBuilder getBuilder() { + if (builder == null) { + setBuilder(); + } + return builder; + } + + public String buildUri(Map varsMap) { + URI uri = getBuilder().buildFromMap(varsMap); + if (uri != null) { + return uri.toString(); + } else { + return ""; + } + } + +} \ 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 index 000000000..c91f93d4d --- /dev/null +++ b/services/common/src/main/java/org/collectionspace/services/common/UriTemplateBuilder.java @@ -0,0 +1,76 @@ +/** + * 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; + +public class UriTemplateBuilder { + + UriTemplateType uriTemplateType = null; + final static String RESOURCE_TEMPLATE_PATTERN = + "/{servicename}/{identifier}"; + // FIXME: Get static strings below (e.g. "items") from already-declared + // constants elsewhere + final static String ITEM_TEMPLATE_PATTERN = + "/{servicename}/{identifier}/items/{itemIdentifier}"; + final static String CONTACT_TEMPLATE_PATTERN = + "/{servicename}/{identifier}/items/{itemIdentifier}/contacts/{contactIdentifier}"; + final static UriTemplate RESOURCE_URI_TEMPLATE = + new UriTemplate(RESOURCE_TEMPLATE_PATTERN); + final static UriTemplate ITEM_URI_TEMPLATE = + new UriTemplate(ITEM_TEMPLATE_PATTERN); + final static UriTemplate CONTACT_URI_TEMPLATE = + new UriTemplate(CONTACT_TEMPLATE_PATTERN); + + public UriTemplateBuilder(UriTemplateType type) { + this.uriTemplateType = type; + } + + public UriTemplateType getType() { + return this.uriTemplateType; + } + + @Override + public String toString() { + return "URI Builder of type " + getType().toString(); + } + + public UriTemplate getURITemplate() { + switch (uriTemplateType) { + case RESOURCE: + return RESOURCE_URI_TEMPLATE; + + case ITEM: + return ITEM_URI_TEMPLATE; + + case CONTACT: + return CONTACT_URI_TEMPLATE; + + default: + return RESOURCE_URI_TEMPLATE; + } + } + + public enum UriTemplateType { + + RESOURCE, ITEM, CONTACT + }; +} \ No newline at end of file