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.servlet; 20 21 import java.util.Collections; 22 import java.util.HashMap; 23 import java.util.Map; 24 25 import org.springframework.extensions.webscripts.FormatReader; 26 import org.springframework.extensions.webscripts.WebScriptException; 27 import org.springframework.extensions.webscripts.WebScriptRequest; 28 import org.springframework.extensions.webscripts.WebScriptResponse; 29 30 31 /** 32 * Convert multipart/formdata to class org.alfresco.web.scripts.servlet.FormData 33 * 34 * @author davidc 35 */ 36 public class FormDataReader implements FormatReader<FormData> 37 { 38 39 /* (non-Javadoc) 40 * @see org.alfresco.web.scripts.FormatReader#getSourceMimetype() 41 */ 42 public String getSourceMimetype() 43 { 44 return "multipart/form-data"; 45 } 46 47 /* (non-Javadoc) 48 * @see org.alfresco.web.scripts.FormatReader#getDestinationClass() 49 */ 50 public Class<FormData> getDestinationClass() 51 { 52 return FormData.class; 53 } 54 55 /* (non-Javadoc) 56 * @see org.alfresco.web.scripts.FormatReader#read(org.alfresco.web.scripts.WebScriptRequest) 57 */ 58 public FormData read(WebScriptRequest req) 59 { 60 if (!(req instanceof WebScriptServletRequest)) 61 { 62 throw new WebScriptException("Failed to convert request to FormData"); 63 } 64 return new FormData(((WebScriptServletRequest)req).getHttpServletRequest()); 65 } 66 67 /* (non-Javadoc) 68 * @see org.alfresco.web.scripts.FormatReader#createScriptParameters(org.alfresco.web.scripts.WebScriptRequest, org.alfresco.web.scripts.WebScriptResponse) 69 */ 70 public Map<String, Object> createScriptParameters(WebScriptRequest req, WebScriptResponse res) 71 { 72 Map<String, Object> params = new HashMap<String, Object>(); 73 // Because form data is 'special', the request may have already parsed 74 // it, so ask the request for the cached content 75 params.put("formdata", req.parseContent()); 76 return params; 77 } 78 }