]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
be3df4641ded54e25d5afbdc0b40bda9ae3ce4f1
[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.MultivaluedMap;
29
30 import org.collectionspace.services.common.security.UnauthorizedException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * RemoteServiceContextImpl
36  *
37  * $LastChangedRevision: $
38  * $LastChangedDate: $
39  */
40 public class RemoteServiceContextImpl<IT, OT>
41         extends AbstractServiceContextImpl<IT, OT>
42         implements RemoteServiceContext<IT, OT> {
43
44     /** The logger. */
45     final Logger logger = LoggerFactory.getLogger(RemoteServiceContextImpl.class);
46     //input stores original content as received over the wire
47     /** The input. */
48     private IT input;    
49     /** The output. */
50     private OT output;
51     /** The target of the HTTP request **/
52     JaxRsContext jaxRsContext;
53     
54     @Override
55     public void setJaxRsContext(JaxRsContext theJaxRsContext) {
56         this.jaxRsContext = theJaxRsContext;
57     }
58     
59     @Override
60     public JaxRsContext getJaxRsContext() {
61         return this.jaxRsContext;
62     }
63
64     /**
65      * Instantiates a new remote service context impl.
66      * 
67      * @param serviceName the service name
68      * 
69      * @throws UnauthorizedException the unauthorized exception
70      */
71     protected RemoteServiceContextImpl(String serviceName) throws UnauthorizedException {
72         super(serviceName);
73     }
74
75     /**
76      * Instantiates a new remote service context impl. (This is "package" protected for the Factory class)
77      * 
78      * @param serviceName the service name
79      * 
80      * @throws UnauthorizedException the unauthorized exception
81      */
82     protected RemoteServiceContextImpl(String serviceName, IT theInput) throws UnauthorizedException {
83         this(serviceName);
84         this.input = theInput;        
85     }
86
87     /**
88      * Instantiates a new remote service context impl. (This is "package" protected for the Factory class)
89      * 
90      * @param serviceName the service name
91      * @param theInput the the input
92      * @param queryParams the query params
93      * 
94      * @throws UnauthorizedException the unauthorized exception
95      */
96     protected RemoteServiceContextImpl(String serviceName,
97                 IT theInput,
98                 MultivaluedMap<String, String> queryParams) throws UnauthorizedException {
99         this(serviceName, theInput);
100         this.setQueryParams(queryParams);
101     }
102
103     /* (non-Javadoc)
104      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getInput()
105      */
106     @Override
107     public IT getInput() {
108         return input;
109     }
110
111     /* (non-Javadoc)
112      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setInput(java.lang.Object)
113      */
114     @Override
115     public void setInput(IT input) {
116         //for security reasons, do not allow to set input again (from handlers)
117         if (this.input != null) {
118             String msg = "Non-null input cannot be set!";
119             logger.error(msg);
120             throw new IllegalStateException(msg);
121         }
122         this.input = input;
123     }
124
125     /* (non-Javadoc)
126      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#getOutput()
127      */
128     @Override
129     public OT getOutput() {
130         return output;
131     }
132
133     /* (non-Javadoc)
134      * @see org.collectionspace.services.common.context.AbstractServiceContextImpl#setOutput(java.lang.Object)
135      */
136     @Override
137     public void setOutput(OT output) {
138         this.output = output;
139     }
140
141     /* (non-Javadoc)
142      * @see org.collectionspace.services.common.context.RemoteServiceContext#getLocalContext(java.lang.String)
143      */
144     @Override
145     public ServiceContext getLocalContext(String localContextClassName) throws Exception {
146         ClassLoader cloader = Thread.currentThread().getContextClassLoader();
147         Class ctxClass = cloader.loadClass(localContextClassName);
148         if (!ServiceContext.class.isAssignableFrom(ctxClass)) {
149             throw new IllegalArgumentException("getLocalContext requires "
150                     + " implementation of " + ServiceContext.class.getName());
151         }
152
153         Constructor ctor = ctxClass.getConstructor(java.lang.String.class);
154         ServiceContext ctx = (ServiceContext) ctor.newInstance(getServiceName());
155         return ctx;
156     }    
157 }