]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e9dd0574432c4773b54a7230cca3fa77f4f042ee
[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     public String getRepositoryName() throws Exception {
121         String result = null;
122         
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);
130         } else {
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);
134         }
135         
136         return result;
137     }
138     
139     /* (non-Javadoc)
140      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getInput()
141      */
142     @Override
143     public IT getInput() {
144         return input;
145     }
146
147     /* (non-Javadoc)
148      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setInput(java.lang.Object)
149      */
150     @Override
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.";
155             logger.error(msg);
156             throw new IllegalStateException(msg);
157         }
158         this.input = input;
159     }
160
161     /* (non-Javadoc)
162      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getOutput()
163      */
164     @Override
165     public OT getOutput() {
166         return output;
167     }
168
169     /* (non-Javadoc)
170      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setOutput(java.lang.Object)
171      */
172     @Override
173     public void setOutput(OT output) {
174         this.output = output;
175     }
176
177     /**
178      * Return the JAX-RS resource for the current context.
179      * 
180      * @param ctx
181      * @return
182      * @throws Exception 
183      */
184     public CollectionSpaceResource<IT, OT> getResource(ServiceContext ctx) throws Exception {
185         CollectionSpaceResource<IT, OT> result = null;
186         
187         ResourceMap<IT, OT> resourceMap = ctx.getResourceMap();
188         String resourceName = ctx.getClient().getServiceName();
189         result = (CollectionSpaceResource<IT, OT>) resourceMap.get(resourceName);
190         
191         return result;
192     }
193     
194     /**
195      * @return the map of service names to resource classes.
196      */
197     @Override
198     public ResourceMap getResourceMap() {
199         ResourceMap result = resourceMap;
200         
201         if (result == null) {
202                 result = ServiceMain.getInstance().getJaxRSResourceMap();
203         }
204         
205         return result;
206     }
207     
208     /**
209      * @param map the map of service names to resource instances.
210      */
211     public void setResourceMap(ResourceMap map) {
212         this.resourceMap = map;
213     }
214
215  
216     
217     /* (non-Javadoc)
218      * @see org.collectionspace.services.common.context.RemoteServiceContext#getLocalContext(java.lang.String)
219      */
220     @Override
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());
227         }
228
229         Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
230         ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());
231         return ctx;
232     }
233
234         @Override
235         public CollectionSpaceResource<IT, OT> getResource() throws Exception {
236                 // TODO Auto-generated method stub
237                 throw new RuntimeException("Unimplemented method.");
238         }
239
240         @Override
241         public CollectionSpaceResource<IT, OT> getResource(String serviceName)
242                         throws Exception {
243                 // TODO Auto-generated method stub
244                 throw new RuntimeException("Unimplemented method.");
245         }
246 }