1 | package com.ether; |
---|
2 | |
---|
3 | import com.ether.WebException.WebCode; |
---|
4 | import com.ether.user.User; |
---|
5 | import net.sf.json.JSONObject; |
---|
6 | import org.apache.commons.logging.Log; |
---|
7 | import org.apache.commons.logging.LogFactory; |
---|
8 | import org.jetbrains.annotations.NotNull; |
---|
9 | import org.jetbrains.annotations.Nullable; |
---|
10 | |
---|
11 | import javax.servlet.ServletOutputStream; |
---|
12 | import javax.servlet.http.HttpServletRequest; |
---|
13 | import javax.servlet.http.HttpServletResponse; |
---|
14 | import java.io.IOException; |
---|
15 | import java.io.PrintWriter; |
---|
16 | import java.security.InvalidParameterException; |
---|
17 | |
---|
18 | public final class WebHelper |
---|
19 | { |
---|
20 | private WebHelper() |
---|
21 | { |
---|
22 | } |
---|
23 | |
---|
24 | @NotNull |
---|
25 | public static String getRequestParameter( @NotNull final HttpServletRequest httpServletRequest, @Nullable final String parameterName, @Nullable final String defaultParameterValue, final boolean canBeNull ) |
---|
26 | throws InvalidParameterException |
---|
27 | { |
---|
28 | String parameterValue; |
---|
29 | if( null == parameterName ) |
---|
30 | parameterValue = defaultParameterValue; |
---|
31 | |
---|
32 | parameterValue = httpServletRequest.getParameter( parameterName ); |
---|
33 | |
---|
34 | if( parameterValue == null ) |
---|
35 | parameterValue = defaultParameterValue; |
---|
36 | |
---|
37 | if( !canBeNull && parameterValue == null ) |
---|
38 | throw new InvalidParameterException( parameterName ); |
---|
39 | |
---|
40 | return parameterValue; |
---|
41 | } |
---|
42 | |
---|
43 | public static void writeJsonToResponse( @NotNull final HttpServletResponse httpServletResponse, @NotNull final String data ) |
---|
44 | throws IOException |
---|
45 | { |
---|
46 | httpServletResponse.setCharacterEncoding( UTF8Charset.getEncoding() ); |
---|
47 | httpServletResponse.setContentType( CONTENT_TYPE_TEXT_JSON ); |
---|
48 | |
---|
49 | final PrintWriter writer = httpServletResponse.getWriter(); |
---|
50 | writer.write( data ); |
---|
51 | } |
---|
52 | |
---|
53 | public static void displayAjaxError( @NotNull final HttpServletResponse response, @Nullable final String methodName ) |
---|
54 | throws WebException |
---|
55 | { |
---|
56 | try |
---|
57 | { |
---|
58 | response.setStatus( HttpServletResponse.SC_NOT_IMPLEMENTED ); |
---|
59 | response.setCharacterEncoding( UTF8Charset.getEncoding() ); |
---|
60 | response.setContentType( "text/plain;charset=" + UTF8Charset.getEncoding() ); |
---|
61 | final ServletOutputStream outputStream = response.getOutputStream(); |
---|
62 | outputStream.print( "error.method_not_implemented : " + methodName + " is not implemented." ); |
---|
63 | outputStream.flush(); |
---|
64 | outputStream.close(); |
---|
65 | } |
---|
66 | catch( IOException e ) |
---|
67 | { |
---|
68 | throw new WebException( WebCode.IO_EXCEPTION_ERROR_TO_GET_OUTPUTSTREAM, e ); |
---|
69 | } |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * This method returns the user in session if there is one |
---|
74 | * |
---|
75 | * @param request |
---|
76 | * @return |
---|
77 | */ |
---|
78 | public static JSONObject getJSONUser( @NotNull final HttpServletRequest request ) |
---|
79 | { |
---|
80 | final JSONObject jSONPeople = new JSONObject(); |
---|
81 | |
---|
82 | final User user = (User) request.getSession().getAttribute( "SES_USER" ); |
---|
83 | if( null == user ) |
---|
84 | return null; |
---|
85 | |
---|
86 | jSONPeople.put( "name", user.getLastName() ); |
---|
87 | jSONPeople.put( "firstName", user.getFirstName() ); |
---|
88 | jSONPeople.put( "role", user.getRole() ); |
---|
89 | return jSONPeople; |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | private static final Log LOGGER = LogFactory.getLog( WebHelper.class ); |
---|
94 | |
---|
95 | public static final int STATUS_CODE_SERVICE_EXCEPTION = 500; |
---|
96 | private static final String CONTENT_TYPE_TEXT_JSON = "text/javascript;charset=" + UTF8Charset.getEncoding(); |
---|
97 | } |
---|