]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b0cfee337ce10c7d5cb8e923f5d2160347f50e6e
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.common.context;
25
26 import java.lang.reflect.Constructor;
27 import javax.ws.rs.core.UriInfo;
28
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;
36
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 /**
41  * RemoteServiceContextImpl
42  *
43  * $LastChangedRevision: $
44  * $LastChangedDate: $
45  */
46 public class RemoteServiceContextImpl<IT, OT>
47         extends AbstractServiceContextImpl<IT, OT>
48         implements RemoteServiceContext<IT, OT> {
49
50     /** The logger. */
51     final Logger logger = LoggerFactory.getLogger(RemoteServiceContextImpl.class);
52     //input stores original content as received over the wire
53     /** The input. */
54     private IT input;    
55     /** The output. */
56     private OT output;
57     /** The target of the HTTP request **/
58     JaxRsContext jaxRsContext;
59     
60     ResourceMap resourceMap = null;
61     
62     @Override
63     public void setJaxRsContext(JaxRsContext theJaxRsContext) {
64         this.jaxRsContext = theJaxRsContext;
65     }
66     
67     @Override
68     public JaxRsContext getJaxRsContext() {
69         return this.jaxRsContext;
70     }
71
72     /**
73      * Instantiates a new remote service context impl.
74      * 
75      * @param serviceName the service name
76      * 
77      * @throws UnauthorizedException the unauthorized exception
78      */
79     protected RemoteServiceContextImpl(String serviceName, UriInfo uriInfo) throws UnauthorizedException {
80         super(serviceName, uriInfo);
81     }
82
83     /**
84      * Instantiates a new remote service context impl. (This is "package" protected for the Factory class)
85      * 
86      * @param serviceName the service name
87      * 
88      * @throws UnauthorizedException the unauthorized exception
89      */
90     protected RemoteServiceContextImpl(String serviceName, IT theInput, UriInfo uriInfo) throws UnauthorizedException {
91         this(serviceName, uriInfo);
92         this.input = theInput;        
93     }
94
95     /**
96      * Instantiates a new remote service context impl. (This is "package" protected for the Factory class)
97      * 
98      * @param serviceName the service name
99      * @param theInput the the input
100      * @param queryParams the query params
101      * 
102      * @throws UnauthorizedException the unauthorized exception
103      */
104     protected RemoteServiceContextImpl(String serviceName,
105                 IT theInput,
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());
113         }
114     }
115
116     /*
117      * Returns the name of the service's acting repository.  Gets this from the tenant and service bindings files
118      */
119     public String getRepositoryName() throws Exception {
120         String result = null;
121         
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);
129         } else {
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);
133         }
134         
135         return result;
136     }
137     
138     /* (non-Javadoc)
139      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getInput()
140      */
141     @Override
142     public IT getInput() {
143         return input;
144     }
145
146     /* (non-Javadoc)
147      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setInput(java.lang.Object)
148      */
149     @Override
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 = "Non-null input cannot be set!";
154             logger.error(msg);
155             throw new IllegalStateException(msg);
156         }
157         this.input = input;
158     }
159
160     /* (non-Javadoc)
161      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getOutput()
162      */
163     @Override
164     public OT getOutput() {
165         return output;
166     }
167
168     /* (non-Javadoc)
169      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setOutput(java.lang.Object)
170      */
171     @Override
172     public void setOutput(OT output) {
173         this.output = output;
174     }
175
176     /**
177      * @return the map of service names to resource classes.
178      */
179     public ResourceMap getResourceMap() {
180         return resourceMap;
181     }
182
183     /**
184      * @param map the map of service names to resource instances.
185      */
186     public void setResourceMap(ResourceMap map) {
187         this.resourceMap = map;
188     }
189
190  
191     
192     /* (non-Javadoc)
193      * @see org.collectionspace.services.common.context.RemoteServiceContext#getLocalContext(java.lang.String)
194      */
195     @Override
196     public ServiceContext getLocalContext(String localContextClassName) throws Exception {
197         ClassLoader cloader = Thread.currentThread().getContextClassLoader();
198         Class<?> ctxClass = cloader.loadClass(localContextClassName);
199         if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
200             throw new IllegalArgumentException("getLocalContext requires "
201                     + " implementation of " + ServiceContext.class.getName());
202         }
203
204         Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
205         ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());
206         return ctx;
207     }
208 }