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
120 public String getRepositoryName() throws Exception {
121 String result = null;
123 TenantBindingConfigReaderImpl tenantBindingConfigReader = ServiceMain.getInstance().getTenantBindingConfigReader();
124 String tenantId = this.getTenantId();
125 TenantBindingType tenantBindingType = tenantBindingConfigReader.getTenantBinding(tenantId);
126 ServiceBindingType serviceBindingType = this.getServiceBinding();
127 String servicesRepoDomainName = serviceBindingType.getRepositoryDomain();
128 if (servicesRepoDomainName != null && servicesRepoDomainName.trim().isEmpty() == false) {
129 result = ConfigUtils.getRepositoryName(tenantBindingType, servicesRepoDomainName);
131 String errMsg = String.format("The '%s' service for tenant ID=%s did not declare a repository domain in its service bindings.",
132 serviceBindingType.getName(), tenantId);
133 throw new Exception(errMsg);
140 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getInput()
143 public IT getInput() {
148 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setInput(java.lang.Object)
151 public void setInput(IT input) {
152 //for security reasons, do not allow to set input again (from handlers)
153 if (this.input != null) {
154 String msg = "Resetting or changing an context's input is not allowed.";
156 throw new IllegalStateException(msg);
162 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getOutput()
165 public OT getOutput() {
170 * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setOutput(java.lang.Object)
173 public void setOutput(OT output) {
174 this.output = output;
178 * Return the JAX-RS resource for the current context.
184 public CollectionSpaceResource<IT, OT> getResource(ServiceContext ctx) throws Exception {
185 CollectionSpaceResource<IT, OT> result = null;
187 ResourceMap<IT, OT> resourceMap = ctx.getResourceMap();
188 String resourceName = ctx.getClient().getServiceName();
189 result = (CollectionSpaceResource<IT, OT>) resourceMap.get(resourceName);
195 * @return the map of service names to resource classes.
198 public ResourceMap getResourceMap() {
199 ResourceMap result = resourceMap;
201 if (result == null) {
202 result = ServiceMain.getInstance().getJaxRSResourceMap();
209 * @param map the map of service names to resource instances.
211 public void setResourceMap(ResourceMap map) {
212 this.resourceMap = map;
218 * @see org.collectionspace.services.common.context.RemoteServiceContext#getLocalContext(java.lang.String)
221 public ServiceContext getLocalContext(String localContextClassName) throws Exception {
222 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
223 Class<?> ctxClass = cloader.loadClass(localContextClassName);
224 if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
225 throw new IllegalArgumentException("getLocalContext requires "
226 + " implementation of " + ServiceContext.class.getName());
229 Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
230 ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());
235 public CollectionSpaceResource<IT, OT> getResource() throws Exception {
236 // TODO Auto-generated method stub
237 throw new RuntimeException("Unimplemented method.");
241 public CollectionSpaceResource<IT, OT> getResource(String serviceName)
243 // TODO Auto-generated method stub
244 throw new RuntimeException("Unimplemented method.");