Imported changesets 6ad7cf34a8f5, d535941636f3, f3a4422f7b1a from the default branch
[pithos] / src / gr / ebs / gss / server / BaseServlet.java
1 /*
2  * Copyright 2008, 2009, 2010 Electronic Business Systems Ltd.
3  *
4  * This file is part of GSS.
5  *
6  * GSS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 package gr.ebs.gss.server;
20
21 import static gr.ebs.gss.server.configuration.GSSConfigurationFactory.getConfiguration;
22 import gr.ebs.gss.client.exceptions.RpcException;
23 import gr.ebs.gss.server.ejb.ExternalAPI;
24
25 import java.io.UnsupportedEncodingException;
26 import java.net.URLEncoder;
27 import java.util.Formatter;
28
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32 import javax.rmi.PortableRemoteObject;
33 import javax.servlet.http.HttpServlet;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 /**
39  * The base servlet contains a collection of helper methods.
40  *
41  * @author past
42  */
43 public class BaseServlet extends HttpServlet {
44         /**
45          * The request parameter name for the GWT code server URL, used when
46          * debugging.
47          */
48         protected static final String GWT_SERVER_PARAM = "gwt.codesvr";
49
50         /**
51          * The serial version UID of the class.
52          */
53         private static final long serialVersionUID = 1L;
54
55         /**
56          * The logger.
57          */
58         private static Log logger = LogFactory.getLog(BaseServlet.class);
59
60         /**
61          * A helper method that retrieves a reference to the ExternalAPI bean and
62          * stores it for future use.
63          *
64          * @return an ExternalAPI instance
65          * @throws RpcException in case an error occurs
66          */
67         protected ExternalAPI getService() throws RpcException {
68                 try {
69                         final Context ctx = new InitialContext();
70                         final Object ref = ctx.lookup(getConfiguration().getString("externalApiPath"));
71                         return (ExternalAPI) PortableRemoteObject.narrow(ref, ExternalAPI.class);
72                 } catch (final NamingException e) {
73                         logger.error("Unable to retrieve the ExternalAPI EJB", e);
74                         throw new RpcException("An error occurred while contacting the naming service");
75                 }
76         }
77
78         /**
79          * Return the name of the service.
80          */
81         protected String getServiceName() {
82                 return getConfiguration().getString("serviceName", "GSS");
83         }
84
85         /**
86          * Decode the request attribute provided by the container to a UTF-8
87          * string, since GSS assumes all data to be encoded in UTF-8. The
88          * servlet container's encoding can be specified in gss.properties.
89          */
90         protected String decodeAttribute(Object attribute) throws UnsupportedEncodingException {
91                 return new String(attribute.toString().getBytes(getConfiguration().getString("requestAttributeEncoding")), "UTF-8");
92         }
93
94         /**
95          * A helper method that converts a byte buffer to a printable list of
96          * hexadecimal numbers.
97          */
98         protected String getHexString(byte[] buffer) {
99                 StringBuilder sb = new StringBuilder();
100                 Formatter formatter = new Formatter(sb);
101                 for (int i=0; i<buffer.length; i++)
102                         formatter.format("0x%x, ", buffer[i]);
103                 return sb.toString();
104         }
105
106         protected String encode(String parameter) throws UnsupportedEncodingException {
107                 return URLEncoder.encode(parameter, "UTF-8");
108         }
109 }