1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.springframework.extensions.webscripts;
20
21 import javax.servlet.http.HttpServletResponse;
22
23 import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.springframework.extensions.webscripts.Description.FormatStyle;
26
27
28
29
30
31
32
33 public abstract class WebScriptRequestImpl implements WebScriptRequest
34 {
35 protected static final Log logger = LogFactory.getLog(WebScriptRequestImpl.class);
36 private Runtime runtime;
37
38
39 private Object requestContent = this;
40
41
42
43
44
45
46
47 public WebScriptRequestImpl(Runtime runtime)
48 {
49 this.runtime = runtime;
50 }
51
52
53
54
55 public Runtime getRuntime()
56 {
57 return runtime;
58 }
59
60
61
62
63 public String getExtensionPath()
64 {
65 String extensionPath = "";
66 Match match = getServiceMatch();
67 if (match != null)
68 {
69 String matchPath = getServiceMatch().getPath();
70 String pathInfo = getPathInfo();
71 if (pathInfo != null)
72 {
73 extensionPath = pathInfo.substring(matchPath.length());
74 }
75 }
76 return extensionPath;
77 }
78
79
80
81
82 public boolean isGuest()
83 {
84 return Boolean.valueOf(getParameter("guest"));
85 }
86
87
88
89
90 public String getFormat()
91 {
92 String format = null;
93 Match match = getServiceMatch();
94 if (match != null)
95 {
96 Description desc = match.getWebScript().getDescription();
97 FormatStyle style = desc.getFormatStyle();
98
99
100 if (style == FormatStyle.extension || style == FormatStyle.any)
101 {
102 String pathInfo = getPathInfo();
103 if (pathInfo != null)
104 {
105 int extIdx = pathInfo.lastIndexOf('.');
106 if (extIdx != -1)
107 {
108
109 int pathIdx = pathInfo.lastIndexOf('/');
110 if (pathIdx < extIdx)
111 {
112 format = pathInfo.substring(extIdx +1);
113 }
114 }
115 }
116 }
117
118
119 if (style == FormatStyle.argument || style == FormatStyle.any)
120 {
121 String argFormat = getParameter("format");
122 if (argFormat != null)
123 {
124 if (format != null && format.length() > 0)
125 {
126 throw new WebScriptException("Format specified both in extension and format argument");
127 }
128 format = argFormat;
129 }
130 }
131
132
133 if (format == null || format.length() == 0)
134 {
135 String accept = getHeader("Accept");
136 NegotiatedFormat[] negotiatedFormats = desc.getNegotiatedFormats();
137 if (accept != null && negotiatedFormats != null)
138 {
139 if (logger.isDebugEnabled())
140 logger.debug("Negotiating format for " + accept);
141
142 format = NegotiatedFormat.negotiateFormat(accept, negotiatedFormats);
143 if (format == null)
144 {
145 throw new WebScriptException(HttpServletResponse.SC_NOT_ACCEPTABLE, "Cannot negotiate appropriate response format for Accept: " + accept);
146 }
147 }
148 }
149
150
151 if (format == null || format.length() == 0)
152 {
153 format = desc.getDefaultFormat();
154 }
155 }
156
157 return (format == null || format.length() == 0) ? "" : format;
158 }
159
160
161
162
163 public FormatStyle getFormatStyle()
164 {
165 Match match = getServiceMatch();
166 if (match == null)
167 {
168 return FormatStyle.any;
169 }
170 FormatStyle style = match.getWebScript().getDescription().getFormatStyle();
171 if (style != FormatStyle.any)
172 {
173 return style;
174 }
175 else
176 {
177 String argFormat = getParameter("format");
178 if (argFormat != null && argFormat.length() > 0)
179 {
180 return FormatStyle.argument;
181 }
182 else
183 {
184 return FormatStyle.extension;
185 }
186 }
187 }
188
189
190
191
192 public String getJSONCallback()
193 {
194 return getParameter("alf_callback");
195 }
196
197
198
199
200 public boolean forceSuccessStatus()
201 {
202 return false;
203 }
204
205
206
207
208 public String getContentType()
209 {
210 String contentType = getHeader("Content-Type");
211 if (contentType != null && contentType.startsWith("multipart/form-data"))
212 {
213 contentType = "multipart/form-data";
214 }
215 return contentType;
216 }
217
218
219
220
221 public Object parseContent()
222 {
223 if (requestContent == this)
224 {
225 requestContent = null;
226 String contentType = getContentType();
227 if (contentType != null && contentType.length() > 0)
228 {
229 FormatRegistry formatRegistry = getRuntime().getContainer().getFormatRegistry();
230 FormatReader<Object> reader = formatRegistry.getReader(contentType);
231 if (reader != null)
232 {
233 if (logger.isDebugEnabled())
234 logger.debug("Converting request (mimetype: " + contentType + ") to " + reader.getDestinationClass().getName());
235
236 requestContent = reader.read(this);
237 }
238 }
239 }
240 return requestContent;
241 }
242
243 }