1 /* 2 * Copyright 2013 smartics, Kronseder & Reiner GmbH 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package de.smartics.properties.admin.resources.representation.share; 17 18 import java.net.URI; 19 20 import javax.ws.rs.core.Link; 21 import javax.ws.rs.core.UriBuilder; 22 import javax.ws.rs.core.UriInfo; 23 24 import de.smartics.properties.admin.domain.model.ApiServices.ServiceDescriptor; 25 import de.smartics.resteasy.hypermedia.renderer.LinkDescriptor; 26 import de.smartics.resteasy.hypermedia.renderer.LinkDescriptorJaxrs; 27 import de.smartics.resteasy.hypermedia.renderer.LinkMetadata; 28 import de.smartics.resteasy.hypermedia.renderer.LocalizationProvider; 29 30 /** 31 * A simple helper to create {@link LinkDescriptor}s. 32 */ 33 public final class LinkDescriptorFactory 34 { 35 // ********************************* Fields ********************************* 36 37 // --- constants ------------------------------------------------------------ 38 39 // --- members -------------------------------------------------------------- 40 41 /** 42 * The helper to construct links. 43 */ 44 private final UriInfo uriInfo; 45 46 /** 47 * The provider of localized information. 48 */ 49 private final LocalizationProvider provider; 50 51 // ****************************** Initializer ******************************* 52 53 // ****************************** Constructors ****************************** 54 55 /** 56 * Default constructor. 57 * 58 * @param uriInfo the helper to construct links. 59 * @param provider the provider of localized information. 60 */ 61 public LinkDescriptorFactory(final UriInfo uriInfo, 62 final LocalizationProvider provider) 63 { 64 this.uriInfo = uriInfo; 65 this.provider = provider; 66 } 67 68 // ****************************** Inner Classes ***************************** 69 70 // ********************************* Methods ******************************** 71 72 // --- init ----------------------------------------------------------------- 73 74 // --- get&set -------------------------------------------------------------- 75 76 /** 77 * Returns the helper to construct links. 78 * 79 * @return the helper to construct links. 80 */ 81 public UriInfo getUriInfo() 82 { 83 return uriInfo; 84 } 85 86 /** 87 * Returns the provider of localized information. 88 * 89 * @return the provider of localized information. 90 */ 91 public LocalizationProvider getProvider() 92 { 93 return provider; 94 } 95 96 // --- business ------------------------------------------------------------- 97 98 /** 99 * Creates the link descriptor to the a generic service. 100 * 101 * @param id the identifier of the resource to lookup link descriptor 102 * metadata. 103 * @param type the type of the resource. 104 * @param method the method within the resource. 105 * @param params the parameters to the resource URI. 106 * @return the requested link descriptor. 107 */ 108 public LinkDescriptor createDescriptor(final String id, final Class<?> type, 109 final String method, final Object... params) 110 { 111 final UriBuilder builder = uriInfo.getBaseUriBuilder().path(type, method); 112 final URI uri; 113 if (params == null || params.length == 0) 114 { 115 uri = builder.build(); 116 } 117 else 118 { 119 uri = builder.build(params, false); 120 } 121 122 return createDescriptor(id, uri); 123 } 124 125 /** 126 * Creates the link descriptor to the a generic service with a fixed, maybe 127 * external, URI. 128 * 129 * @param id the identifier of the resource to lookup link descriptor 130 * metadata. 131 * @param uri the URI to the link. 132 * @return the requested link descriptor. 133 */ 134 public LinkDescriptor createDescriptor(final String id, final URI uri) 135 { 136 final Link link = Link.fromUri(uri).build(); 137 final LinkMetadata metadata = provider.getLinkMetadata(id); 138 final LinkDescriptor desc = new LinkDescriptorJaxrs(link, metadata); 139 return desc; 140 } 141 142 /** 143 * Creates the link descriptor to the a generic service. 144 * 145 * @param service the service to create a link to. 146 * @param method the method of the service to link to. 147 * @return the requested link descriptor. 148 */ 149 public LinkDescriptor createDescriptor(final ServiceDescriptor service, 150 final String method) 151 { 152 final LinkDescriptor descriptor = 153 createDescriptor(service.getId(), service.getResourceType(), method); 154 return descriptor; 155 } 156 157 // --- object basics -------------------------------------------------------- 158 159 }