2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
24 package org.collectionspace.services.common.context;
26 import java.lang.reflect.Constructor;
28 import javax.ws.rs.core.UriInfo;
30 import org.collectionspace.services.common.CollectionSpaceResource;
31 import org.collectionspace.services.common.ResourceMap;
32 import org.collectionspace.services.common.ServiceMain;
33 import org.collectionspace.services.common.config.ConfigUtils;
34 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
35 import org.collectionspace.services.common.security.UnauthorizedException;
36 import org.collectionspace.services.config.service.ServiceBindingType;
37 import org.collectionspace.services.config.tenant.TenantBindingType;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * RemoteServiceContextImpl
44 * $LastChangedRevision: $
47 public class RemoteServiceContextImpl<IT, OT>
48 extends AbstractServiceContextImpl<IT, OT>
49 implements RemoteServiceContext<IT, OT> {
52 final Logger logger = LoggerFactory.getLogger(RemoteServiceContextImpl.class);
53 //input stores original content as received over the wire
58 /** The target of the HTTP request **/
59 JaxRsContext jaxRsContext;
61 ResourceMap resourceMap = null;
64 public void setJaxRsContext(JaxRsContext theJaxRsContext) {
65 this.jaxRsContext = theJaxRsContext;
69 public JaxRsContext getJaxRsContext() {
70 return this.jaxRsContext;
74 * Instantiates a new remote service context impl.
76 * @param serviceName the service name
78 * @throws UnauthorizedException the unauthorized exception
80 protected RemoteServiceContextImpl(String serviceName, UriInfo uriInfo) throws UnauthorizedException {
81 super(serviceName, uriInfo);
85 * Instantiates a new remote service context impl. (This is "package" protected for the Factory class)
87 * @param serviceName the service name
89 * @throws UnauthorizedException the unauthorized exception
91 protected RemoteServiceContextImpl(String serviceName, IT theInput, UriInfo uriInfo) throws UnauthorizedException {
92 this(serviceName, uriInfo);
93 this.input = theInput;
97 * Instantiates a new remote service context impl. (This is "package" protected for the Factory class)
99 * @param serviceName the service name
100 * @param theInput the the input
101 * @param queryParams the query params
103 * @throws UnauthorizedException the unauthorized exception
105 protected RemoteServiceContextImpl(String serviceName,
107 ResourceMap resourceMap,
108 UriInfo uriInfo) throws UnauthorizedException {
109 this(serviceName, theInput, uriInfo);
110 this.setResourceMap(resourceMap);
111 this.setUriInfo(uriInfo);
112 if (uriInfo != null) {
113 this.setQueryParams(uriInfo.getQueryParameters());
118 * Returns the name of the service's acting repository. Gets this from the tenant and service bindings files
121 public String getRepositoryName() throws Exception {
122 String result = null;
124 TenantBindingConfigReaderImpl tenantBindingConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader();
125 String tenantId = this.getTenantId();
126 TenantBindingType tenantBindingType = tenantBindingConfigReader.getTenantBinding(tenantId);
127 ServiceBindingType serviceBindingType = this.getServiceBinding();
128 String servicesRepoDomainName = serviceBindingType.getRepositoryDomain();
129 if (servicesRepoDomainName != null && servicesRepoDomainName.trim().isEmpty() == false) {
130 result = ConfigUtils.getRepositoryName(tenantBindingType, servicesRepoDomainName);
132 String errMsg = String.format("The '%s' service for tenant ID=%s did not declare a repository domain in its service bindings.",
133 serviceBindingType.getName(), tenantId);
134 throw new Exception(errMsg);
141 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getInput()
144 public IT getInput() {
149 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setInput(java.lang.Object)
152 public void setInput(IT input) {
153 //for security reasons, do not allow to set input again (from handlers)
154 if (this.input != null) {
155 String msg = "Resetting or changing an context's input is not allowed.";
157 throw new IllegalStateException(msg);
163 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getOutput()
166 public OT getOutput() {
171 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setOutput(java.lang.Object)
174 public void setOutput(OT output) {
175 this.output = output;
179 * Return the JAX-RS resource for the current context.
185 public CollectionSpaceResource<IT, OT> getResource(ServiceContext<?, ?> ctx) throws Exception {
186 CollectionSpaceResource<IT, OT> result = null;
188 ResourceMap resourceMap = ctx.getResourceMap();
189 String resourceName = ctx.getClient().getServiceName();
190 result = (CollectionSpaceResource<IT, OT>) resourceMap.get(resourceName);
196 * @return the map of service names to resource classes.
199 public ResourceMap getResourceMap() {
200 ResourceMap result = resourceMap;
202 if (result == null) {
203 result = ServiceMain.getInstance().getJaxRSResourceMap();
210 * @param map the map of service names to resource instances.
213 public void setResourceMap(ResourceMap map) {
214 this.resourceMap = map;
220 * @see org.collectionspace.services.common.context.RemoteServiceContext#getLocalContext(java.lang.String)
223 public ServiceContext<IT, OT> getLocalContext(String localContextClassName) throws Exception {
224 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
225 Class<?> ctxClass = cloader.loadClass(localContextClassName);
226 if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
227 throw new IllegalArgumentException("getLocalContext requires "
228 + " implementation of " + ServiceContext.class.getName());
231 Constructor<?> ctor = ctxClass.getConstructor(java.lang.String.class);
232 ServiceContext<IT, OT> ctx = (ServiceContext<IT, OT>) ctor.newInstance(getServiceName());
237 public CollectionSpaceResource<IT, OT> getResource() throws Exception {
238 // TODO Auto-generated method stub
239 throw new RuntimeException("Unimplemented method.");
243 public CollectionSpaceResource<IT, OT> getResource(String serviceName)
245 // TODO Auto-generated method stub
246 throw new RuntimeException("Unimplemented method.");