Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / server / BaseServlet.java @ 28be8c05

History | View | Annotate | Download (3.4 kB)

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