View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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   * Basic Implementation of a Web Script Request
30   * 
31   * @author davidc
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      // parsed request content
39      private Object requestContent = this;
40  
41  
42      /**
43       * Construct
44       * 
45       * @param runtime
46       */
47      public WebScriptRequestImpl(Runtime runtime)
48      {
49          this.runtime = runtime;
50      }
51      
52      /* (non-Javadoc)
53       * @see org.alfresco.web.scripts.WebScriptRequest#getRuntime()
54       */
55      public Runtime getRuntime()
56      {
57          return runtime;
58      }
59      
60      /* (non-Javadoc)
61       * @see org.alfresco.web.scripts.WebScriptRequest#getExtensionPath()
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      /* (non-Javadoc)
80       * @see org.alfresco.web.scripts.WebScriptRequest#isGuest()
81       */
82      public boolean isGuest()
83      {
84          return Boolean.valueOf(getParameter("guest"));
85      }
86  
87      /* (non-Javadoc)
88       * @see org.alfresco.web.scripts.WebScriptRequest#getFormat()
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              // extract format from extension
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                         // format extension is only valid as the last URL element 
109                         int pathIdx = pathInfo.lastIndexOf('/');
110                         if (pathIdx < extIdx)
111                         {
112                             format = pathInfo.substring(extIdx +1);
113                         }
114                     }
115                 }
116             }
117             
118             // extract format from argument
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             // negotiate format, if necessary
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             // fallback to default
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     /* (non-Javadoc)
161      * @see org.alfresco.web.scripts.WebScriptRequest#getFormatStyle()
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     /* (non-Javadoc)
190      * @see org.alfresco.web.scripts.WebScriptRequest#getJSONCallback()
191      */
192     public String getJSONCallback()
193     {
194         return getParameter("alf_callback");
195     }
196 
197     /* (non-Javadoc)
198      * @see org.alfresco.web.scripts.WebScriptRequest#forceSuccessStatus()
199      */
200     public boolean forceSuccessStatus()
201     {
202         return false;
203     }
204     
205     /* (non-Javadoc)
206      * @see org.alfresco.web.scripts.WebScriptRequest#getContentType()
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     /* (non-Javadoc)
219      * @see org.alfresco.web.scripts.WebScriptRequest#parseContent()
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 }