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;
27 import javax.ws.rs.core.UriInfo;
29 import org.collectionspace.services.common.ResourceMap;
30 import org.collectionspace.services.common.ServiceMain;
31 import org.collectionspace.services.common.config.ConfigUtils;
32 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
33 import org.collectionspace.services.common.security.UnauthorizedException;
34 import org.collectionspace.services.config.service.ServiceBindingType;
35 import org.collectionspace.services.config.tenant.TenantBindingType;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * RemoteServiceContextImpl
43 * $LastChangedRevision: $
46 public class RemoteServiceContextImpl<IT, OT>
47 extends AbstractServiceContextImpl<IT, OT>
48 implements RemoteServiceContext<IT, OT> {
51 final Logger logger = LoggerFactory.getLogger(RemoteServiceContextImpl.class);
52 //input stores original content as received over the wire
57 /** The target of the HTTP request **/
58 JaxRsContext jaxRsContext;
60 ResourceMap resourceMap = null;
63 public void setJaxRsContext(JaxRsContext theJaxRsContext) {
64 this.jaxRsContext = theJaxRsContext;
68 public JaxRsContext getJaxRsContext() {
69 return this.jaxRsContext;
73 * Instantiates a new remote service context impl.
75 * @param serviceName the service name
77 * @throws UnauthorizedException the unauthorized exception
79 protected RemoteServiceContextImpl(String serviceName, UriInfo uriInfo) throws UnauthorizedException {
80 super(serviceName, uriInfo);
84 * Instantiates a new remote service context impl. (This is "package" protected for the Factory class)
86 * @param serviceName the service name
88 * @throws UnauthorizedException the unauthorized exception
90 protected RemoteServiceContextImpl(String serviceName, IT theInput, UriInfo uriInfo) throws UnauthorizedException {
91 this(serviceName, uriInfo);
92 this.input = theInput;
96 * Instantiates a new remote service context impl. (This is "package" protected for the Factory class)
98 * @param serviceName the service name
99 * @param theInput the the input
100 * @param queryParams the query params
102 * @throws UnauthorizedException the unauthorized exception
104 protected RemoteServiceContextImpl(String serviceName,
106 ResourceMap resourceMap,
107 UriInfo uriInfo) throws UnauthorizedException {
108 this(serviceName, theInput, uriInfo);
109 this.setResourceMap(resourceMap);
110 this.setUriInfo(uriInfo);
111 if (uriInfo != null) {
112 this.setQueryParams(uriInfo.getQueryParameters());
117 * Returns the name of the service's acting repository. Gets this from the tenant and service bindings files
119 public String getRepositoryName() throws Exception {
120 String result = null;
122 TenantBindingConfigReaderImpl tenantBindingConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader();
123 String tenantId = this.getTenantId();
124 TenantBindingType tenantBindingType = tenantBindingConfigReader.getTenantBinding(tenantId);
125 ServiceBindingType serviceBindingType = this.getServiceBinding();
126 String servicesRepoDomainName = serviceBindingType.getRepositoryDomain();
127 if (servicesRepoDomainName != null && servicesRepoDomainName.trim().isEmpty() == false) {
128 result = ConfigUtils.getRepositoryName(tenantBindingType, servicesRepoDomainName);
130 String errMsg = String.format("The '%s' service for tenant ID=%s did not declare a repository domain in its service bindings.",
131 serviceBindingType.getName(), tenantId);
132 throw new Exception(errMsg);
139 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getInput()
142 public IT getInput() {
147 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setInput(java.lang.Object)
150 public void setInput(IT input) {
151 //for security reasons, do not allow to set input again (from handlers)
152 if (this.input != null) {
153 String msg = "Resetting or changing an context's input is not allowed.";
155 throw new IllegalStateException(msg);
161 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getOutput()
164 public OT getOutput() {
169 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setOutput(java.lang.Object)
172 public void setOutput(OT output) {
173 this.output = output;
177 * @return the map of service names to resource classes.
179 public ResourceMap getResourceMap() {
180 ResourceMap result = resourceMap;
182 if (result == null) {
183 result = ServiceMain.getInstance().getJaxRSResourceMap();
190 * @param map the map of service names to resource instances.
192 public void setResourceMap(ResourceMap map) {
193 this.resourceMap = map;
199 * @see org.collectionspace.services.common.context.RemoteServiceContext#getLocalContext(java.lang.String)
202 public ServiceContext getLocalContext(String localContextClassName) throws Exception {
203 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
204 Class<?> ctxClass = cloader.loadClass(localContextClassName);
205 if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
206 throw new IllegalArgumentException("getLocalContext requires "
207 + " implementation of " + ServiceContext.class.getName());
210 Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
211 ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());