]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0b1a66a5bca571aad5ed1e33cc7135cd109eb920
[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
28 import javax.ws.rs.core.UriInfo;
29
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;
40
41 /**
42  * RemoteServiceContextImpl
43  *
44  * $LastChangedRevision: $
45  * $LastChangedDate: $
46  */
47 public class RemoteServiceContextImpl<IT, OT>
48         extends AbstractServiceContextImpl<IT, OT>
49         implements RemoteServiceContext<IT, OT> {
50
51     /** The logger. */
52     final Logger logger = LoggerFactory.getLogger(RemoteServiceContextImpl.class);
53     //input stores original content as received over the wire
54     /** The input. */
55     private IT input;    
56     /** The output. */
57     private OT output;
58     /** The target of the HTTP request **/
59     JaxRsContext jaxRsContext;
60     
61     ResourceMap resourceMap = null;
62     
63     @Override
64     public void setJaxRsContext(JaxRsContext theJaxRsContext) {
65         this.jaxRsContext = theJaxRsContext;
66     }
67     
68     @Override
69     public JaxRsContext getJaxRsContext() {
70         return this.jaxRsContext;
71     }
72
73     /**
74      * Instantiates a new remote service context impl.
75      * 
76      * @param serviceName the service name
77      * 
78      * @throws UnauthorizedException the unauthorized exception
79      */
80     protected RemoteServiceContextImpl(String serviceName, UriInfo uriInfo) throws UnauthorizedException {
81         super(serviceName, uriInfo);
82     }
83
84     /**
85      * Instantiates a new remote service context impl. (This is "package" protected for the Factory class)
86      * 
87      * @param serviceName the service name
88      * 
89      * @throws UnauthorizedException the unauthorized exception
90      */
91     protected RemoteServiceContextImpl(String serviceName, IT theInput, UriInfo uriInfo) throws UnauthorizedException {
92         this(serviceName, uriInfo);
93         this.input = theInput;        
94     }
95
96     /**
97      * Instantiates a new remote service context impl. (This is "package" protected for the Factory class)
98      * 
99      * @param serviceName the service name
100      * @param theInput the the input
101      * @param queryParams the query params
102      * 
103      * @throws UnauthorizedException the unauthorized exception
104      */
105     protected RemoteServiceContextImpl(String serviceName,
106                 IT theInput,
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());
114         }
115     }
116
117     /*
118      * Returns the name of the service's acting repository.  Gets this from the tenant and service bindings files
119      */
120     @Override
121         public String getRepositoryName() throws Exception {
122         String result = null;
123         
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);
131         } else {
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);
135         }
136         
137         return result;
138     }
139     
140     /* (non-Javadoc)
141      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getInput()
142      */
143     @Override
144     public IT getInput() {
145         return input;
146     }
147
148     /* (non-Javadoc)
149      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setInput(java.lang.Object)
150      */
151     @Override
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.";
156             logger.error(msg);
157             throw new IllegalStateException(msg);
158         }
159         this.input = input;
160     }
161
162     /* (non-Javadoc)
163      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getOutput()
164      */
165     @Override
166     public OT getOutput() {
167         return output;
168     }
169
170     /* (non-Javadoc)
171      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setOutput(java.lang.Object)
172      */
173     @Override
174     public void setOutput(OT output) {
175         this.output = output;
176     }
177
178     /**
179      * Return the JAX-RS resource for the current context.
180      * 
181      * @param ctx
182      * @return
183      * @throws Exception 
184      */
185     public CollectionSpaceResource<IT, OT> getResource(ServiceContext<?, ?> ctx) throws Exception {
186         CollectionSpaceResource<IT, OT> result = null;
187         
188         ResourceMap resourceMap = ctx.getResourceMap();
189         String resourceName = ctx.getClient().getServiceName();
190         result = (CollectionSpaceResource<IT, OT>) resourceMap.get(resourceName);
191         
192         return result;
193     }
194     
195     /**
196      * @return the map of service names to resource classes.
197      */
198     @Override
199     public ResourceMap getResourceMap() {
200         ResourceMap result = resourceMap;
201         
202         if (result == null) {
203                 result = ServiceMain.getInstance().getJaxRSResourceMap();
204         }
205         
206         return result;
207     }
208     
209     /**
210      * @param map the map of service names to resource instances.
211      */
212     @Override
213         public void setResourceMap(ResourceMap map) {
214         this.resourceMap = map;
215     }
216
217  
218     
219     /* (non-Javadoc)
220      * @see org.collectionspace.services.common.context.RemoteServiceContext#getLocalContext(java.lang.String)
221      */
222     @Override
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());
229         }
230
231         Constructor<?> ctor = ctxClass.getConstructor(java.lang.String.class);
232         ServiceContext<IT, OT> ctx = (ServiceContext<IT, OT>) ctor.newInstance(getServiceName());
233         return ctx;
234     }
235
236         @Override
237         public CollectionSpaceResource<IT, OT> getResource() throws Exception {
238                 // TODO Auto-generated method stub
239                 throw new RuntimeException("Unimplemented method.");
240         }
241
242         @Override
243         public CollectionSpaceResource<IT, OT> getResource(String serviceName)
244                         throws Exception {
245                 // TODO Auto-generated method stub
246                 throw new RuntimeException("Unimplemented method.");
247         }
248 }