Statistics
| Branch: | Tag: | Revision:

root / src / gr / ebs / gss / server / BaseServlet.java @ 672:163de098320c

History | View | Annotate | Download (2.8 kB)

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
import gr.ebs.gss.server.ejb.ExternalAPIBean;
25

    
26
import java.io.UnsupportedEncodingException;
27
import java.net.URLEncoder;
28
import java.util.Formatter;
29

    
30
import javax.servlet.http.HttpServlet;
31

    
32
/**
33
 * The base servlet contains a collection of helper methods.
34
 *
35
 * @author past
36
 */
37
public class BaseServlet extends HttpServlet {
38
        /**
39
         * The request parameter name for the GWT code server URL, used when
40
         * debugging.
41
         */
42
        protected static final String GWT_SERVER_PARAM = "gwt.codesvr";
43

    
44
        /**
45
         * The serial version UID of the class.
46
         */
47
        private static final long serialVersionUID = 1L;
48

    
49
        /**
50
         * A helper method that retrieves a reference to the ExternalAPI bean and
51
         * stores it for future use.
52
         *
53
         * @return an ExternalAPI instance
54
         * @throws RpcException in case an error occurs
55
         */
56
        protected ExternalAPI getService() throws RpcException {
57
                return new ExternalAPIBean();
58
        }
59

    
60
        /**
61
         * Return the name of the service.
62
         */
63
        protected String getServiceName() {
64
                return getConfiguration().getString("serviceName", "GSS");
65
        }
66

    
67
        /**
68
         * Decode the request attribute provided by the container to a UTF-8
69
         * string, since GSS assumes all data to be encoded in UTF-8. The
70
         * servlet container's encoding can be specified in gss.properties.
71
         */
72
        protected String decodeAttribute(Object attribute) throws UnsupportedEncodingException {
73
                return new String(attribute.toString().getBytes(getConfiguration().getString("requestAttributeEncoding")), "UTF-8");
74
        }
75

    
76
        /**
77
         * A helper method that converts a byte buffer to a printable list of
78
         * hexadecimal numbers.
79
         */
80
        protected String getHexString(byte[] buffer) {
81
                StringBuilder sb = new StringBuilder();
82
                Formatter formatter = new Formatter(sb);
83
                for (int i=0; i<buffer.length; i++)
84
                        formatter.format("0x%x, ", buffer[i]);
85
                return sb.toString();
86
        }
87

    
88
        protected String encode(String parameter) throws UnsupportedEncodingException {
89
                return URLEncoder.encode(parameter, "UTF-8");
90
        }
91
}