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
20   123   8   5
6   58   0.4   4
4     2  
1    
 
  GETTransformer       Line # 37 20 0% 8 3 90% 0.9
 
  (9)
 
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.text.MessageFormat;
22    import java.util.Collection;
23    import java.util.Iterator;
24    import java.util.Map;
25   
26   
27    import org.apache.commons.collections.CollectionUtils;
28    import org.apache.commons.configuration.ConfigurationException;
29    import org.apache.log4j.Logger;
30   
31    // TODO: Auto-generated Javadoc
32    /**
33    * The Class TransformForGET.
34    *
35    * @author georgosn
36    */
 
37    public class GETTransformer extends BaseRequestTransformer {
38   
39    /** The Constant LOGGER. */
40    private static final Logger LOGGER = Logger.getLogger(POSTTransformer.class
41    .getName());
42   
43    /** The Constant QUERY_FORMAT. */
44    private final static String QUERY_FORMAT = "{0}={1}";
45   
46    /**
47    * Instantiates a new gets the transformer.
48    *
49    * @param com the com
50    */
 
51  11 toggle public GETTransformer(final ConnectorCommand com) {
52  11 super(com);
53    }
54   
55    /**
56    * Format query parameters.
57    *
58    * @param urlParameternames the url parameternames
59    * @param params the params
60    * @return the string
61    */
 
62  7 toggle protected String formatQueryParameters(
63    final Collection<String> urlParameternames,
64    final Map<String, Object> params) {
65  7 final StringBuilder queryParammeters = new StringBuilder();
66   
67  7 final Iterator<String> paramNames = CollectionUtils.disjunction(
68    params.keySet(), urlParameternames).iterator();
69  15 while (paramNames.hasNext()) {
70  8 final String name = paramNames.next();
71  8 queryParammeters.append(MessageFormat.format(QUERY_FORMAT,
72    new Object[] { name, params.get(name) }));
73  8 if (paramNames.hasNext()) {
74  4 queryParammeters.append("&");
75    }
76    }
77  7 return queryParammeters.toString();
78    }
79   
80    /**
81    * Prepare query.
82    *
83    * @param request the request
84    * @param context the context
85    * @param args the args
86    * @throws CommandException the command exception
87    */
 
88  7 toggle @Override
89    public void prepareUri(final ConnectorRequest request,
90    final ConnectorContext context, final Object[] args)
91    throws CommandException {
92   
93  7 try {
94  7 final Object[] arguments = new Object[] {
95    context.getService().getServiceURL(request.getParameters()),
96    formatQueryParameters(context.getService()
97    .getURLParameters(), request.getParameters()) };
98  7 super.prepareUri(request, context, arguments);
99   
100    } catch (final ConfigurationException e) {
101  0 LOGGER.error("Error preparing query", e);
102  0 addError(context, "Configuration of service contains errors");
103  0 throw new CommandException("Error preparing query", e);
104    }
105    }
106   
107    /* (non-Javadoc)
108    * @see net.sf.arestc.core.commands.RequestTransformer#transform(net.sf.arestc.core.ConnectorRequest, net.sf.arestc.core.ConnectorContext)
109    */
 
110  7 toggle public void transform(final ConnectorRequest request,
111    final ConnectorContext context) throws CommandException {
112   
113  7 prepareUri(request, context, null);
114   
115  7 context.setMethod(context.getService().getMethod()
116    .getMethod(context.getServiceUri().toString()));
117  7 if (context.getService().isAuthenticationRequired()) {
118  3 super.authenticate(request, context);
119    }
120   
121  7 return;
122    }
123    }