Clover Coverage Report - ARESTC 0.1.5
Coverage timestamp: Tue Aug 10 2010 10:22:22 CEST
../../../../../img/srcFileCovDistChart8.png 55% of files have more coverage
51   260   22   5.67
14   127   0.43   9
9     2.44  
1    
 
  TransformToServiceCall       Line # 51 51 0% 22 20 73% 0.7297297
 
  (27)
 
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.commands;
20   
21    import java.io.File;
22    import java.io.FileNotFoundException;
23    import java.net.URI;
24    import java.net.URISyntaxException;
25    import java.text.MessageFormat;
26    import java.util.Iterator;
27    import java.util.Map;
28    import java.util.Map.Entry;
29   
30    import net.sf.arestc.core.BaseCommand;
31    import net.sf.arestc.core.BaseError;
32    import net.sf.arestc.core.CommandException;
33    import net.sf.arestc.core.ConnectorContext;
34    import net.sf.arestc.core.ConnectorRequest;
35    import net.sf.arestc.core.services.RESTService;
36    import net.sf.arestc.core.validations.RequestValidator;
37   
38    import org.apache.commons.configuration.ConfigurationException;
39    import org.apache.commons.httpclient.methods.RequestEntity;
40    import org.apache.commons.httpclient.methods.multipart.FilePart;
41    import org.apache.commons.httpclient.methods.multipart.FilePartSource;
42    import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
43    import org.apache.commons.httpclient.methods.multipart.Part;
44    import org.apache.commons.httpclient.methods.multipart.StringPart;
45    import org.apache.log4j.Logger;
46   
47    // TODO: Auto-generated Javadoc
48    /**
49    * The Class TransformToServiceCall.
50    */
 
51    abstract class TransformToServiceCall extends BaseCommand {
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 ERROR_OCCURED_IN_PROCESS. */
57    private static final String ERROR_OCCURED_IN_PROCESS = "Error occured in process:";
58   
59    /** The Constant LOGGER. */
60    private static final Logger LOGGER = Logger.getLogger(TransformToServiceCall.class.getName());
61   
62    /** The Constant QUERY_FORMAT. */
63    private final static String QUERY_FORMAT = "{0}={1}";
64   
65    /** The validator. */
66    private final RequestValidator validator;
67   
68    /**
69    * Instantiates a new transform to service call.
70    */
 
71  280 toggle public TransformToServiceCall() {
72   
73  280 validator = new RequestValidator();
74    }
75   
76    /**
77    * Adds the error.
78    *
79    * @param context
80    * the context
81    * @param ErrorMsg
82    * the error msg
83    */
 
84  0 toggle private void addError(final ConnectorContext context, final String ErrorMsg) {
85  0 context.getErrors().add(new BaseError(-1, this, ErrorMsg, context.getService()));
86    }
87   
88    /**
89    * Built request entity.
90    *
91    * @param request
92    * the request
93    * @param context
94    * the context
95    * @return the request entity
96    */
 
97  3 toggle protected RequestEntity builtRequestEntity(final ConnectorRequest request,
98    final ConnectorContext context) {
99  3 RequestEntity ret = null;
100   
101  3 final Iterator<Entry<String, Object>> bodyParametersIterator = request.getBODYParameters(context).entrySet().iterator();
102  3 final Part[] parts = new Part[request.getBODYParameters(context).size()];
103  3 int partNum = 0;
104  5 while (bodyParametersIterator.hasNext()) {
105  2 final Entry<String, Object> entry = bodyParametersIterator.next();
106  2 if (File.class.isAssignableFrom(entry.getValue().getClass())) {
107  2 try {
108   
109  2 parts[partNum++] = new FilePart(entry.getKey(), new FilePartSource((File) entry.getValue()));
110   
111    } catch (final FileNotFoundException e) {
112  0 LOGGER.error("file could not be found", e);
113    // just skip this file
114    }
115    } else {
116  0 parts[partNum++] = new StringPart(entry.getKey(), entry.getValue().toString());
117   
118    }
119    }
120  3 if (partNum > 0) {
121   
122  1 ret = new MultipartRequestEntity(parts, context.getMethod().getParams());
123   
124    }
125  3 return ret;
126    }
127   
128    /**
129    * Format query parameters.
130    *
131    * @param service
132    * the service
133    * @param params
134    * the params
135    * @return the string
136    */
 
137  7 toggle protected String formatQueryParameters(final RESTService service,
138    final Map<String, Object> params) {
139  7 final StringBuilder queryParammeters = new StringBuilder();
140   
141  7 final Iterator<Entry<String, Object>> entries = params.entrySet().iterator();
142  24 while (entries.hasNext()) {
143  17 final Entry<String, Object> entry = entries.next();
144  17 final Object serviceParamType = service.getParameters().get(entry.getKey());
145  17 if (null == serviceParamType
146    || "query".equalsIgnoreCase((String) serviceParamType)) {
147  8 queryParammeters.append(MessageFormat.format(QUERY_FORMAT, new Object[] {
148    entry.getKey(), entry.getValue() }));
149  8 if (entries.hasNext()) {
150  4 queryParammeters.append("&");
151    }
152    }
153    }
154  7 return queryParammeters.toString();
155    }
156   
157    /**
158    * Gets the validator.
159    *
160    * @return the validator
161    */
 
162  10 toggle public RequestValidator getValidator() {
163  10 return validator;
164    }
165   
166    /**
167    * Prepare body.
168    *
169    * @param request
170    * the request
171    * @param context
172    * the context
173    * @throws CommandException
174    * the command exception
175    */
 
176  3 toggle protected void prepareBody(final ConnectorRequest request,
177    final ConnectorContext context) throws CommandException {
178   
179  3 request.setBody(builtRequestEntity(request, context));
180   
181    }
182   
183    /**
184    * Prepare query.
185    *
186    * @param request
187    * the request
188    * @param context
189    * the context
190    * @throws CommandException
191    * the command exception
192    */
 
193  7 toggle protected void prepareQuery(final ConnectorRequest request,
194    final ConnectorContext context) throws CommandException {
195   
196  7 try {
197  7 final Object[] arguments = new Object[] {
198    context.getService().getServiceURL(request.getParameters()),
199    formatQueryParameters(context.getService(), request.getParameters()) };
200  7 final StringBuffer uri = new StringBuffer(MessageFormat.format(context.getService().getMethod().getURLPattern(), arguments));
201   
202  7 context.setServiceUri(new URI(uri.toString()));
203   
204    } catch (final URISyntaxException e) {
205  0 LOGGER.error(ERROR_OCCURED_IN_PROCESS, e);
206  0 addError(context, "URI Syntax for the service was wrong");
207  0 throw new CommandException(ERROR_WHILE_TRYING_TO_CREATE_URI_FOR_THE_SERVICE, e);
208    } catch (final ConfigurationException e) {
209  0 LOGGER.error(ERROR_OCCURED_IN_PROCESS, e);
210  0 addError(context, "Configuration of service contains errors");
211  0 throw new CommandException(ERROR_WHILE_TRYING_TO_CREATE_URI_FOR_THE_SERVICE, e);
212    }
213    }
214   
215    /**
216    * Prepare uri.
217    *
218    * @param request
219    * the request
220    * @param context
221    * the context
222    * @throws CommandException
223    * the command exception
224    */
 
225  3 toggle protected void prepareUri(final ConnectorRequest request,
226    final ConnectorContext context) throws CommandException {
227  3 try {
228  3 final StringBuffer uri = new StringBuffer(MessageFormat.format(context.getService().getMethod().getURLPattern(), new Object[] { context.getService().getServiceURL(request.getURLParameters(context)) }));
229   
230  3 context.setServiceUri(new URI(uri.toString()));
231    } catch (final URISyntaxException e) {
232  0 LOGGER.error(ERROR_OCCURED_IN_PROCESS, e);
233  0 addError(context, "URI Syntax for the service was wrong");
234  0 throw new CommandException(ERROR_WHILE_TRYING_TO_CREATE_URI_FOR_THE_SERVICE, e);
235    } catch (final ConfigurationException e) {
236  0 LOGGER.error(ERROR_OCCURED_IN_PROCESS, e);
237  0 addError(context, "Configuration of service contains errors");
238  0 throw new CommandException(ERROR_WHILE_TRYING_TO_CREATE_URI_FOR_THE_SERVICE, e);
239    }
240    }
241   
242    /*
243    * (non-Javadoc)
244    *
245    * @see
246    * net.sf.arestc.core.BaseCommand#process(net.sf.arestc.core.ConnectorRequest
247    * , net.sf.arestc.core.ConnectorContext)
248    */
 
249  10 toggle @Override
250    public ConnectorContext process(final ConnectorRequest request,
251    final ConnectorContext context) throws CommandException {
252   
253  10 if (!validator.validateRequest(request, context)) {
254  0 context.add(new BaseError(-1, this, "Validation failed, look for configuration discrepancies with the actual runtime feeds", null));
255  0 return context;
256    }
257   
258  10 return super.process(request, context);
259    }
260    }