2 * Copyright 2008, 2009, 2010 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.server;
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;
25 import java.io.UnsupportedEncodingException;
26 import java.net.URLEncoder;
27 import java.util.Formatter;
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;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
39 * The base servlet contains a collection of helper methods.
43 public class BaseServlet extends HttpServlet {
45 * The request parameter name for the GWT code server URL, used when
48 protected static final String GWT_SERVER_PARAM = "gwt.codesvr";
51 * The serial version UID of the class.
53 private static final long serialVersionUID = 1L;
58 private static Log logger = LogFactory.getLog(BaseServlet.class);
61 * A helper method that retrieves a reference to the ExternalAPI bean and
62 * stores it for future use.
64 * @return an ExternalAPI instance
65 * @throws RpcException in case an error occurs
67 protected ExternalAPI getService() throws RpcException {
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");
79 * Return the name of the service.
81 protected String getServiceName() {
82 return getConfiguration().getString("serviceName", "GSS");
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.
90 protected String decodeAttribute(Object attribute) throws UnsupportedEncodingException {
91 return new String(attribute.toString().getBytes(getConfiguration().getString("requestAttributeEncoding")), "UTF-8");
95 * A helper method that converts a byte buffer to a printable list of
96 * hexadecimal numbers.
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();
106 protected String encode(String parameter) throws UnsupportedEncodingException {
107 return URLEncoder.encode(parameter, "UTF-8");