Clover Coverage Report - ARESTC 0.1.7-SNAPSHOT
Coverage timestamp: Fri Aug 27 2010 19:12:04 CEST
../../../../img/srcFileCovDistChart7.png 67% of files have more coverage
41   190   14   10.25
14   108   0.34   4
4     3.5  
1    
 
  POSTTransformer       Line # 45 41 0% 14 22 62.7% 0.62711865
 
  (6)
 
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.core;
20   
21    import java.io.ByteArrayOutputStream;
22    import java.io.File;
23    import java.io.FileNotFoundException;
24    import java.io.IOException;
25    import java.util.Iterator;
26    import java.util.Map.Entry;
27   
28   
29    import org.apache.commons.collections.CollectionUtils;
30    import org.apache.commons.collections.PredicateUtils;
31    import org.apache.commons.configuration.ConfigurationException;
32    import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
33    import org.apache.commons.httpclient.methods.RequestEntity;
34    import org.apache.commons.httpclient.methods.multipart.FilePart;
35    import org.apache.commons.httpclient.methods.multipart.FilePartSource;
36    import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
37    import org.apache.commons.httpclient.methods.multipart.Part;
38    import org.apache.commons.httpclient.methods.multipart.StringPart;
39    import org.apache.log4j.Logger;
40   
41    // TODO: Auto-generated Javadoc
42    /**
43    * The Class TransformForPOST.
44    */
 
45    public class POSTTransformer extends BaseRequestTransformer {
46   
47    /** The Constant CONFIGURATION_OF_SERVICE_CONTAINS_ERRORS. */
48    private static final String CONFIGURATION_OF_SERVICE_CONTAINS_ERRORS = "Configuration of service contains errors";
49   
50    /** The Constant ERROR_OCCURED_IN_PROCESS. */
51    private static final String ERROR_OCCURED_IN_PROCESS = "Error occured in process:";
52   
53    /** The Constant ERROR_WHILE_TRYING_TO_CREATE_URI_FOR_THE_SERVICE. */
54    private static final String ERROR_WHILE_TRYING_TO_CREATE_URI_FOR_THE_SERVICE = "Error while trying to create URI for the service";
55   
56    /** The Constant LOGGER. */
57    private static final Logger LOGGER = Logger.getLogger(POSTTransformer.class
58    .getName());
59   
60    /**
61    * Instantiates a new pOST transformer.
62    *
63    * @param com the com
64    */
 
65  6 toggle public POSTTransformer(final ConnectorCommand com) {
66  6 super(com);
67    }
68   
69    /**
70    * Prepare body.
71    *
72    * @param request
73    * the request
74    * @param context
75    * the context
76    * @throws CommandException
77    * the command exception
78    */
 
79  1 toggle protected void prepareMultipartBody(final ConnectorRequest request,
80    final ConnectorContext context) throws CommandException {
81  1 RequestEntity ret = null;
82   
83  1 final Iterator<Entry<String, Object>> bodyParametersIterator = request
84    .getBODYParameters(context).entrySet().iterator();
85  1 Part[] parts = new Part[request.getBODYParameters(context).size()];
86  1 int partNum = 0;
87  3 while (bodyParametersIterator.hasNext()) {
88  2 final Entry<String, Object> entry = bodyParametersIterator.next();
89  2 if (File.class.isAssignableFrom(entry.getValue().getClass())) {
90  2 try {
91   
92  2 parts[partNum++] = new FilePart(entry.getKey(),
93    new FilePartSource((File) entry.getValue()));
94   
95    } catch (final FileNotFoundException e) {
96  0 LOGGER.error("file could not be found", e);
97    // just skip this file
98    }
99    } else {
100  0 parts[partNum++] = new StringPart(entry.getKey(), entry
101    .getValue().toString());
102   
103    }
104    }
105  1 if (null != request.getBody()) {
106  0 final Part[] temp = new Part[partNum + 1];
107   
108  0 if (partNum > 0) {
109  0 System.arraycopy(parts, 0, temp, 0, parts.length);
110    }
111  0 partNum++;
112  0 try {
113  0 final ByteArrayOutputStream out = new ByteArrayOutputStream();
114  0 request.getBody().writeRequest(out);
115   
116  0 temp[partNum - 1] = new StringPart("bodyPart",
117    out.toString(request.getCharset()));
118  0 parts = new Part[temp.length];
119  0 System.arraycopy(temp, 0, parts, 0, temp.length);
120    } catch (final IOException e) {
121  0 throw new IllegalStateException(e);
122    }
123   
124    }
125  1 if (partNum > 0) {
126   
127  1 ret = new MultipartRequestEntity(parts, context.getMethod()
128    .getParams());
129   
130    }
131  1 request.setBody(ret);
132   
133    }
134   
135    /**
136    * Prepare uri.
137    *
138    * @param request the request
139    * @param context the context
140    * @param args the args
141    * @throws CommandException the command exception
142    */
 
143  3 toggle @Override
144    public void prepareUri(final ConnectorRequest request,
145    final ConnectorContext context, final Object[] args)
146    throws CommandException {
147  3 try {
148   
149  3 super.prepareUri(
150    request,
151    context,
152    new Object[] { context.getService().getServiceURL(
153    request.getURLParameters(context)) });
154   
155    } catch (final ConfigurationException e) {
156  0 LOGGER.error(ERROR_OCCURED_IN_PROCESS, e);
157  0 addError(context, CONFIGURATION_OF_SERVICE_CONTAINS_ERRORS);
158  0 throw new CommandException(
159    ERROR_WHILE_TRYING_TO_CREATE_URI_FOR_THE_SERVICE, e);
160    }
161    }
162   
163    /* (non-Javadoc)
164    * @see net.sf.arestc.core.commands.RequestTransformer#transform(net.sf.arestc.core.ConnectorRequest, net.sf.arestc.core.ConnectorContext)
165    */
 
166  2 toggle public void transform(final ConnectorRequest request,
167    final ConnectorContext context) throws CommandException {
168   
169  2 prepareUri(request, context, null);
170  2 context.setMethod(context.getService().getMethod()
171    .getMethod(context.getServiceUri().toString()));
172   
173  2 if (CollectionUtils.exists(request.getParameters().values(),
174    PredicateUtils.instanceofPredicate(File.class))) {
175  1 prepareMultipartBody(request, context);
176    } else {
177  1 context.getMethod().addRequestHeader("Content-type",
178    context.getService().getContentType());
179    }
180   
181  2 ((EntityEnclosingMethod) context.getMethod()).setRequestEntity(request
182    .getBody());
183   
184  2 if (context.getService().isAuthenticationRequired()) {
185  2 super.authenticate(request, context);
186    }
187   
188  2 return;
189    }
190    }