|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| TransformToServiceCall | Line # 51 | 51 | 0% | 22 | 20 | 73% |
0.7297297
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (27) | |||
| Result | |||
|
0.3918919
|
net.sf.arestc.core.OverallDesignTest.testTicketRetrievalWithTicketer
net.sf.arestc.core.OverallDesignTest.testTicketRetrievalWithTicketer
|
1 PASS | |
|
0.3918919
|
net.sf.arestc.core.OverallDesignTest.testRepositoryMultiPost
net.sf.arestc.core.OverallDesignTest.testRepositoryMultiPost
|
1 PASS | |
|
0.36486486
|
net.sf.arestc.core.OverallDesignTest.testTicketRetrieval
net.sf.arestc.core.OverallDesignTest.testTicketRetrieval
|
1 PASS | |
|
0.2972973
|
net.sf.arestc.core.OverallDesignTest.testRepositoryDelete
net.sf.arestc.core.OverallDesignTest.testRepositoryDelete
|
1 PASS | |
|
0.2972973
|
net.sf.arestc.core.OverallDesignTest.testFileUpload
net.sf.arestc.core.OverallDesignTest.testFileUpload
|
1 PASS | |
|
0.2972973
|
net.sf.arestc.core.OverallDesignTest.testFileDownload
net.sf.arestc.core.OverallDesignTest.testFileDownload
|
1 PASS | |
|
0.2972973
|
net.sf.arestc.core.OverallDesignTest.testRepositoryRetrieval
net.sf.arestc.core.OverallDesignTest.testRepositoryRetrieval
|
1 PASS | |
|
0.2972973
|
net.sf.arestc.core.OverallDesignTest.testRepositoryCreate
net.sf.arestc.core.OverallDesignTest.testRepositoryCreate
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestFullServiceParams
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestFullServiceParams
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestNullParameters
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestNullParameters
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestFullParams
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestFullParams
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.configuration.ServicesConfigurationFactoryTest.testConfigureString
net.sf.arestc.configuration.ServicesConfigurationFactoryTest.testConfigureString
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.configuration.services.BaseServiceTest.testGetAdminUser
net.sf.arestc.configuration.services.BaseServiceTest.testGetAdminUser
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestNullServiceParams
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestNullServiceParams
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.core.OverallDesignTest.testLoadResponseFromFile
net.sf.arestc.core.OverallDesignTest.testLoadResponseFromFile
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestAuthenticationUserCred
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestAuthenticationUserCred
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.configuration.services.BaseServiceTest.testGetAdminPassword
net.sf.arestc.configuration.services.BaseServiceTest.testGetAdminPassword
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestAuthenticationAdminAdminCred
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestAuthenticationAdminAdminCred
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestAuthenticationUserNoCred
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestAuthenticationUserNoCred
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.configuration.services.BaseServiceTest.testGetAuthenticatingUser
net.sf.arestc.configuration.services.BaseServiceTest.testGetAuthenticatingUser
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.configuration.services.BaseServiceTest.testIsAuthenticationRequired
net.sf.arestc.configuration.services.BaseServiceTest.testIsAuthenticationRequired
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.configuration.services.BaseServiceTest.testGetServiceURL
net.sf.arestc.configuration.services.BaseServiceTest.testGetServiceURL
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.configuration.services.BaseServiceTest.testGetURLParameters
net.sf.arestc.configuration.services.BaseServiceTest.testGetURLParameters
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestAuthenticationAdminNoCred
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestAuthenticationAdminNoCred
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.configuration.ServicesConfigurationFactoryTest.testConfigure
net.sf.arestc.configuration.ServicesConfigurationFactoryTest.testConfigure
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestNotNullServiceParams
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestNotNullServiceParams
|
1 PASS | |
|
0.027027028
|
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestAuthenticationNoCred
net.sf.arestc.core.validations.RequestValidatorTest.testValidateRequestAuthenticationNoCred
|
1 PASS | |
| 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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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 |
@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 | } | |
|
||||||||||||