source: ether_megapoli/trunk/common/implementation/com/ether/WebHelper.java @ 130

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

[Visualization] interface quite finish

File size: 3.2 KB
Line 
1package com.ether;
2
3import java.io.IOException;
4import java.io.PrintWriter;
5import java.security.InvalidParameterException;
6import java.util.Locale;
7import java.util.ResourceBundle;
8
9import javax.servlet.ServletOutputStream;
10import javax.servlet.http.HttpServletRequest;
11import javax.servlet.http.HttpServletResponse;
12
13import org.apache.commons.logging.Log;
14import org.apache.commons.logging.LogFactory;
15import org.jetbrains.annotations.NotNull;
16import org.jetbrains.annotations.Nullable;
17
18import com.ether.EtherException.EtherCode;
19
20public final class WebHelper
21{
22    private WebHelper()
23    {
24    }
25
26    @NotNull
27    public static String getRequestParameter( @NotNull final HttpServletRequest httpServletRequest, @Nullable final String parameterName, @Nullable final String defaultParameterValue, final boolean canBeNull )
28            throws InvalidParameterException
29    {
30        String parameterValue;
31        if( null == parameterName )
32            parameterValue = defaultParameterValue;
33
34        parameterValue = httpServletRequest.getParameter( parameterName );
35
36        if( parameterValue == null )
37            parameterValue = defaultParameterValue;
38
39        if( !canBeNull && parameterValue == null )
40            throw new InvalidParameterException( parameterName );
41
42        return parameterValue;
43    }
44
45    public static void writeJsonToResponse( @NotNull final HttpServletResponse httpServletResponse, @NotNull final String data )
46            throws IOException
47    {
48        httpServletResponse.setCharacterEncoding( UTF8Charset.getEncoding() );
49        httpServletResponse.setContentType( CONTENT_TYPE_TEXT_JSON );
50
51        final PrintWriter writer = httpServletResponse.getWriter();
52
53        writer.write( JSON_TAG_BEGIN );
54        writer.write( data );
55        writer.write( JSON_TAG_END );
56    }
57
58    public static void displayAjaxError( @NotNull final HttpServletResponse response, @Nullable final String methodName )
59            throws EtherException
60    {
61        try
62        {
63            response.setStatus( HttpServletResponse.SC_NOT_IMPLEMENTED );
64            response.setCharacterEncoding( UTF8Charset.getEncoding() );
65            response.setContentType( "text/plain;charset=" + UTF8Charset.getEncoding() );
66            final ServletOutputStream outputStream = response.getOutputStream();
67            outputStream.print( "error.method_not_implemented : " + methodName + " is not implemented." );
68            outputStream.flush();
69            outputStream.close();
70        }
71        catch( IOException e )
72        {
73            throw new EtherException( EtherCode.IO_EXCEPTION_ERROR_TO_GET_OUTPUTSTREAM, e );
74        }
75    }
76
77    @NotNull
78    public static ResourceBundle getBundle( @Nullable final Locale locale )
79    {
80        if( null != locale )
81            return ResourceBundle.getBundle( "ApplicationResources", locale );
82        else
83            return ResourceBundle.getBundle( "ApplicationResources" );
84    }
85
86    private static final Log LOGGER = LogFactory.getLog( WebHelper.class );
87
88    private static final String CONTENT_TYPE_TEXT_JSON = "text/javascript;charset=" + UTF8Charset.getEncoding();
89    private static final char JSON_TAG_BEGIN = ' ';  // '(';
90    private static final char JSON_TAG_END = ' ';     // ')';
91}
Note: See TracBrowser for help on using the repository browser.