source: ether_ndacc/trunk/common/implementation/com/ether/WebHelper.java @ 204

Last change on this file since 204 was 204, checked in by vmipsl, 13 years ago

[Internationalisation]

File size: 3.8 KB
Line 
1package com.ether;
2
3import org.apache.commons.logging.Log;
4import org.apache.commons.logging.LogFactory;
5import org.jetbrains.annotations.NotNull;
6import org.jetbrains.annotations.Nullable;
7
8import javax.servlet.ServletOutputStream;
9import javax.servlet.http.HttpServletRequest;
10import javax.servlet.http.HttpServletResponse;
11import java.io.IOException;
12import java.io.PrintWriter;
13import java.security.InvalidParameterException;
14
15public final class WebHelper
16{
17    private WebHelper()
18    {
19    }
20
21    @NotNull
22    public static String getRequestParameter(@NotNull final HttpServletRequest httpServletRequest, @Nullable final String parameterName, @Nullable final String defaultParameterValue, final boolean canBeNull )
23        throws InvalidParameterException
24    {
25        String parameterValue;
26        if(null == parameterName)
27                parameterValue = defaultParameterValue;
28       
29        parameterValue = httpServletRequest.getParameter( parameterName );
30
31        if( parameterValue == null )
32            parameterValue = defaultParameterValue;
33
34        if( !canBeNull && parameterValue == null )
35            throw new InvalidParameterException( parameterName );
36
37        return parameterValue;
38    }
39
40    public static void writeJsonToResponse( @NotNull final HttpServletResponse httpServletResponse, @NotNull final String data )
41            throws IOException
42    {
43        httpServletResponse.setCharacterEncoding( UTF8Charset.getEncoding() );
44        httpServletResponse.setContentType( CONTENT_TYPE_TEXT_JSON );
45
46        final PrintWriter writer = httpServletResponse.getWriter();
47
48        writer.write( JSON_TAG_BEGIN );
49        writer.write( data );
50        writer.write( JSON_TAG_END );
51    }
52
53    public static void displayError( @NotNull final HttpServletResponse response, @Nullable final String methodName )
54        throws EtherException
55    {           
56        try {
57                response.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
58                response.setCharacterEncoding(UTF8Charset.getEncoding());
59                response.setContentType("text/plain;charset="+ UTF8Charset.getEncoding());
60                final ServletOutputStream outputStream = response.getOutputStream();
61                outputStream.print("error.method_not_implemented : " + methodName+ " is not implemented.");
62                outputStream.flush();
63                outputStream.close();
64        } catch (IOException e) {
65                throw new EtherException( EtherException.EtherCode.IO_EXCEPTION_ERROR_TO_GET_OUTPUTSTREAM, e);
66        }
67    }
68
69    /**
70     * This method returns the part of URL after the servlet without the part "language"
71     *
72     * @param request
73     * @return
74     */
75    @NotNull
76    public static String getUrlToRecall( @Nullable final HttpServletRequest request )
77    {
78        if( null == request )
79            return "";
80        else
81        {
82            // TODO : récupérer cette chaine directement à partir du bean localeChangeInterceptor du servlet-context.xml
83            final String localeChangeInterceptorParam = "language";
84
85            final String queryString = request.getQueryString();
86            if( !queryString.contains( localeChangeInterceptorParam ) )
87                return queryString;
88            else
89            {
90                final String[] strings = queryString.split( '&' + localeChangeInterceptorParam );
91                final int indexNextParam = strings[1].indexOf( "&" );
92                if( -1 != indexNextParam )
93                    return strings[0] + strings[1].substring( indexNextParam );
94                else
95                    return strings[0];
96            }
97        }
98    }
99
100    private static final Log LOGGER = LogFactory.getLog( WebHelper.class );
101   
102    private static final String CONTENT_TYPE_TEXT_JSON = "text/javascript;charset=" + UTF8Charset.getEncoding();
103    private static final char JSON_TAG_BEGIN = ' ' ;  // '(';
104    private static final char JSON_TAG_END = ' ';     // ')';
105}
Note: See TracBrowser for help on using the repository browser.