1. Project Clover database Fri Apr 29 2016 13:24:33 CEST
  2. Package info.magnolia.module.blossom.multipart

File BlossomMultipartRequestWrapper.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart4.png
61% of files have more coverage

Code metrics

4
20
4
1
108
50
7
0.35
5
4
1.75

Classes

Class Line # Actions
BlossomMultipartRequestWrapper 58 20 0% 7 17
0.3928571339.3%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /**
2    * This file Copyright (c) 2010-2016 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
32    *
33    */
34    package info.magnolia.module.blossom.multipart;
35   
36    import java.util.LinkedHashMap;
37    import java.util.Map;
38    import javax.servlet.http.HttpServletRequest;
39   
40    import org.slf4j.Logger;
41    import org.slf4j.LoggerFactory;
42    import org.springframework.http.HttpHeaders;
43    import org.springframework.util.Assert;
44    import org.springframework.web.multipart.MultipartFile;
45   
46    import info.magnolia.cms.beans.runtime.Document;
47    import info.magnolia.cms.beans.runtime.MultipartForm;
48   
49    /**
50    * Implementation of MultipartHttpServletRequest that exposes multipart files that has been parsed by the Magnolia
51    * multipart filter. Does not provide handling of request parameters since that's left to the request wrapper
52    * added by Magnolia.
53    *
54    * @see info.magnolia.module.blossom.multipart.BlossomMultipartResolver
55    * @see info.magnolia.cms.beans.runtime.MultipartForm
56    * @since 1.1.1
57    */
 
58    public class BlossomMultipartRequestWrapper extends CompatibleAbstractMultipartHttpServletRequest {
59   
60    private static final String CONTENT_TYPE = "Content-Type";
61   
62    private static final Logger log = LoggerFactory.getLogger(BlossomMultipartRequestWrapper.class);
63   
64    private MultipartForm multipartForm;
65   
 
66  1 toggle public BlossomMultipartRequestWrapper(HttpServletRequest request) {
67  1 super(request);
68   
69  1 multipartForm = (MultipartForm) request.getAttribute(MultipartForm.REQUEST_ATTRIBUTE_NAME);
70   
71    // The request needs to have been wrapped by the Magnolia multipart filter.
72  1 Assert.notNull(multipartForm, "Request is not multipart or not already parsed");
73    }
74   
 
75  1 toggle @Override
76    protected void initializeMultipart() {
77  1 Map<String, MultipartFile> multipartFiles = new LinkedHashMap<String, MultipartFile>();
78  1 for (Object o : multipartForm.getDocuments().entrySet()) {
79  1 Map.Entry entry = (Map.Entry) o;
80  1 multipartFiles.put((String) entry.getKey(), new BlossomMultipartFile((Document) entry.getValue()));
81    }
82  1 try {
83  1 setSingleValueMultipartFiles(multipartFiles);
84    } catch (Exception e) {
85  0 log.error("Unable to initialize multipart request wrapper.", e);
86    }
87    }
88   
89    // Intentionally not annotated with @Override to allow compilation with Spring 3.0
 
90  0 toggle public HttpHeaders getMultipartHeaders(String paramOrFileName) {
91  0 String contentType = getMultipartContentType(paramOrFileName);
92  0 if (contentType == null) {
93  0 return null;
94    }
95  0 HttpHeaders headers = new HttpHeaders();
96  0 headers.add(CONTENT_TYPE, contentType);
97  0 return headers;
98    }
99   
100    // Intentionally not annotated with @Override to allow compilation with Spring 3.0
 
101  0 toggle public String getMultipartContentType(String paramOrFileName) {
102  0 MultipartFile file = getFile(paramOrFileName);
103  0 if (file == null) {
104  0 return null;
105    }
106  0 return file.getContentType();
107    }
108    }