Clover Coverage Report - ARESTC 0.1.7-SNAPSHOT
Coverage timestamp: Fri Aug 27 2010 19:12:04 CEST
../../../../img/srcFileCovDistChart9.png 41% of files have more coverage
18   158   10   2.57
4   69   0.56   3.5
7     1.43  
2    
 
  MULTIPOSTHandler       Line # 45 13 0% 4 4 76.5% 0.7647059
  MULTIPOSTHandler.RequestWrapper       Line # 50 5 0% 6 1 91.7% 0.9166667
 
  (1)
 
1    /*
2    *
3    * (C)opyright 2010, Nikolaos Georgosopoulos
4    *
5    * This file is part of ARESTC.
6    *
7    * ARESTC is free software: you can redistribute it and/or modify it under the
8    * terms of the Lesser General Public License as published by the Free Software
9    * Foundation, either version 3 of the License, or (at your option) any later
10    * version.
11    *
12    * ARESTC is distributed in the hope that it will be useful, but WITHOUT ANY
13    * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14    * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15    *
16    * You should have received a copy of the Lesser General Public License along
17    * with ARESTC. If not, see <http://www.gnu.org/licenses/>.
18    */
19    package net.sf.arestc.testserver;
20   
21    import java.io.IOException;
22    import java.io.InputStream;
23    import java.util.ArrayList;
24    import java.util.List;
25   
26    import org.apache.commons.fileupload.FileItem;
27    import org.apache.commons.fileupload.FileItemFactory;
28    import org.apache.commons.fileupload.FileUploadException;
29    import org.apache.commons.fileupload.RequestContext;
30    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
31    import org.apache.commons.fileupload.servlet.ServletFileUpload;
32    import org.apache.http.HttpEntityEnclosingRequest;
33    import org.apache.http.HttpException;
34    import org.apache.http.HttpRequest;
35    import org.apache.http.HttpResponse;
36    import org.apache.http.HttpStatus;
37    import org.apache.http.entity.BasicHttpEntity;
38    import org.apache.http.protocol.HttpContext;
39    import org.apache.log4j.Logger;
40   
41    // TODO: Auto-generated Javadoc
42    /**
43    * The Class MULTIPOSTHandler.
44    */
 
45    public class MULTIPOSTHandler extends AbstractRequestHandler {
46   
47    /**
48    * The Class RequestWrapper.
49    */
 
50    private static class RequestWrapper implements RequestContext {
51   
52    /** The request. */
53    private final HttpEntityEnclosingRequest request;
54   
55    /**
56    * Instantiates a new request wrapper.
57    *
58    * @param req
59    * the req
60    */
 
61  1 toggle public RequestWrapper(final HttpRequest req) {
62  1 request = (HttpEntityEnclosingRequest) req;
63    }
64   
65    /*
66    * (non-Javadoc)
67    *
68    * @see
69    * org.apache.commons.fileupload.RequestContext#getCharacterEncoding()
70    */
 
71  1 toggle public String getCharacterEncoding() {
72  1 return request.getEntity().getContentEncoding() == null ? "ISO-8859-1"
73    : request.getEntity().getContentEncoding().getValue();
74    }
75   
76    /*
77    * (non-Javadoc)
78    *
79    * @see org.apache.commons.fileupload.RequestContext#getContentLength()
80    */
 
81  1 toggle public int getContentLength() {
82  1 return (int) request.getEntity().getContentLength();
83    }
84   
85    /*
86    * (non-Javadoc)
87    *
88    * @see org.apache.commons.fileupload.RequestContext#getContentType()
89    */
 
90  1 toggle public String getContentType() {
91  1 return request.getEntity().getContentType().getValue();
92    }
93   
94    /*
95    * (non-Javadoc)
96    *
97    * @see org.apache.commons.fileupload.RequestContext#getInputStream()
98    */
 
99  1 toggle public InputStream getInputStream() throws IOException {
100  1 return ((BasicHttpEntity) request.getEntity()).getContent();
101    }
102   
103    }
104   
105    /** The Constant LOGGER. */
106    private static final Logger LOGGER = Logger.getLogger(MULTIPOSTHandler.class
107    .getName());
108   
109    /** The factory. */
110    private final FileItemFactory factory;
111   
112    /** The upload. */
113    private final ServletFileUpload upload;
114   
115    /** The Constant RESPONSE_FILE_NAME. */
116    private static final String RESPONSE_FILE_NAME = "stubs/returned_cmis_create.xml";
117   
118    /** The Constant URL. */
119    public static final String URL = "/alfresco/service/api/node/workspace/SpacesStore/fe424d83-d697-401c-b29e-ce1a915d7283/filemulti";
120   
121    /**
122    * Instantiates a new mULTIPOST handler.
123    */
 
124  1 toggle public MULTIPOSTHandler() {
125  1 factory = new DiskFileItemFactory();
126  1 upload = new ServletFileUpload(factory);
127   
128    }
129   
130    /*
131    * (non-Javadoc)
132    *
133    * @see
134    * net.sf.arestc.testserver.AbstractRequestHandler#handle(org.apache.http
135    * .HttpRequest, org.apache.http.HttpResponse,
136    * org.apache.http.protocol.HttpContext)
137    */
 
138  1 toggle @Override
139    public void handle(final HttpRequest request, final HttpResponse response,
140    final HttpContext context) throws HttpException, IOException {
141  1 if ("POST".equals(request.getRequestLine().getMethod())) {
142   
143  1 response.setStatusCode(HttpStatus.SC_CREATED);
144  1 response.addHeader("Content-Type", "application/xml");
145  1 List<FileItem> items = new ArrayList<FileItem>();
146  1 try {
147  1 items = upload.parseRequest(new RequestWrapper(request));
148    } catch (final FileUploadException e) {
149  0 LOGGER.error(e);
150  0 response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
151    }
152  1 LOGGER.debug(items.size());
153  1 addFileAsBody(response, RESPONSE_FILE_NAME);
154    } else {
155  0 response.setStatusCode(HttpStatus.SC_METHOD_FAILURE);
156    }
157    }
158    }