1 | package com.ether; |
---|
2 | |
---|
3 | import java.io.IOException; |
---|
4 | import java.io.PrintWriter; |
---|
5 | import java.security.InvalidParameterException; |
---|
6 | import java.util.Locale; |
---|
7 | import java.util.ResourceBundle; |
---|
8 | |
---|
9 | import javax.servlet.ServletOutputStream; |
---|
10 | import javax.servlet.http.HttpServletRequest; |
---|
11 | import javax.servlet.http.HttpServletResponse; |
---|
12 | |
---|
13 | import org.apache.commons.logging.Log; |
---|
14 | import org.apache.commons.logging.LogFactory; |
---|
15 | import org.jetbrains.annotations.NotNull; |
---|
16 | import org.jetbrains.annotations.Nullable; |
---|
17 | |
---|
18 | import com.ether.EtherException.EtherCode; |
---|
19 | |
---|
20 | public 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 | } |
---|