Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
PlainJSONScreen |
|
| 1.0;1 |
1 | package org.apache.turbine.modules.screens; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
20 | */ | |
21 | ||
22 | import java.io.OutputStreamWriter; | |
23 | import java.io.PrintWriter; | |
24 | ||
25 | import org.apache.turbine.pipeline.PipelineData; | |
26 | import org.apache.turbine.util.RunData; | |
27 | import org.slf4j.Logger; | |
28 | import org.slf4j.LoggerFactory; | |
29 | ||
30 | ||
31 | /** | |
32 | * A Screen class for dealing with JSON requests. Typically you would | |
33 | * extend this class and override the doOutput() method to use it by setting the JSON output into | |
34 | * rundata.setMessage( serialized ). | |
35 | * As convenience you may use inject in your extended class the Turbine service JsonService | |
36 | * Use {@link PlainJSONSecureAnnotatedScreen} if you need the user to be | |
37 | * logged in or having a special role in prior to executing the functions you provide. | |
38 | * | |
39 | * <p>Here is an example from a subclass: | |
40 | * | |
41 | * <code> | |
42 | * | |
43 | * | |
44 | * public void doOutput(PipelineData pipelineData) throws Exception | |
45 | * { | |
46 | * RunData data = pipelineData.getRunData(); | |
47 | * JSONStrategy strategy = null; | |
48 | * | |
49 | * try | |
50 | * { | |
51 | * strategy = new XYStrategy(); | |
52 | * // the result goes into rundata.message | |
53 | * strategy.execute(data, jsonService); | |
54 | * } | |
55 | * catch ( Exception e ) | |
56 | * { | |
57 | * log.error( "init failed for "+strategy , e); | |
58 | * String msg = new JSONObject().put("error", e.getMessage()).toString(); | |
59 | * data.setMessage( msg ); | |
60 | * } | |
61 | * | |
62 | * super.doOutput(data); | |
63 | * } | |
64 | * </code> | |
65 | * | |
66 | * | |
67 | * @author gk | |
68 | * @version $Id$ | |
69 | */ | |
70 | 0 | public class PlainJSONScreen extends RawScreen |
71 | { | |
72 | protected static final String JSON_TYPE = "application/json;charset=utf-8"; | |
73 | ||
74 | protected final static int BUFFER_SIZE = 4096; | |
75 | ||
76 | 0 | static final Logger log = LoggerFactory.getLogger(PlainJSONScreen.class); |
77 | ||
78 | /** Injected service instance */ | |
79 | //@TurbineService | |
80 | //protected JsonService jsonService; | |
81 | ||
82 | /** | |
83 | * @see org.apache.turbine.modules.screens.RawScreen#getContentType(org.apache.turbine.pipeline.PipelineData) | |
84 | */ | |
85 | @Override | |
86 | protected String getContentType(PipelineData pipelineData) | |
87 | { | |
88 | 0 | return JSON_TYPE; |
89 | } | |
90 | ||
91 | /** | |
92 | * Output JSON content set into {@link RunData#getMessage()}. | |
93 | * | |
94 | * Encoding is UTF-8. @{@link #JSON_TYPE}: {@value #JSON_TYPE}. | |
95 | * | |
96 | * @param pipelineData The PipelineData object. | |
97 | */ | |
98 | @Override | |
99 | protected void doOutput(PipelineData pipelineData) throws Exception | |
100 | { | |
101 | 0 | RunData data = pipelineData.getRunData(); |
102 | // read in json! | |
103 | 0 | String charset = "UTF-8"; //request.getCharacterEncoding(); |
104 | ||
105 | 0 | String json_res =data.getMessage(); |
106 | ||
107 | 0 | log.debug( "json_res output: {}", json_res ); |
108 | 0 | PrintWriter out = new PrintWriter( |
109 | 0 | new OutputStreamWriter(data.getResponse().getOutputStream(),charset)); |
110 | 0 | out.print(json_res.toString()); |
111 | 0 | out.flush(); |
112 | 0 | out.close(); |
113 | 0 | } |
114 | } |