| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 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 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
|
|
|
| 62.7% |
Uncovered Elements: 22 (59) |
Complexity: 14 |
Complexity Density: 0.34 |
|
| 45 |
|
public class POSTTransformer extends BaseRequestTransformer { |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
private static final String CONFIGURATION_OF_SERVICE_CONTAINS_ERRORS = "Configuration of service contains errors"; |
| 49 |
|
|
| 50 |
|
|
| 51 |
|
private static final String ERROR_OCCURED_IN_PROCESS = "Error occured in process:"; |
| 52 |
|
|
| 53 |
|
|
| 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 |
|
|
| 57 |
|
private static final Logger LOGGER = Logger.getLogger(POSTTransformer.class |
| 58 |
|
.getName()); |
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
@param |
| 64 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 65 |
6
|
public POSTTransformer(final ConnectorCommand com) {... |
| 66 |
6
|
super(com); |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
@param |
| 73 |
|
|
| 74 |
|
@param |
| 75 |
|
|
| 76 |
|
@throws |
| 77 |
|
|
| 78 |
|
|
|
|
|
| 50% |
Uncovered Elements: 18 (36) |
Complexity: 8 |
Complexity Density: 0.31 |
|
| 79 |
1
|
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 |
|
|
| 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 |
|
|
| 137 |
|
|
| 138 |
|
@param |
| 139 |
|
@param |
| 140 |
|
@param |
| 141 |
|
@throws |
| 142 |
|
|
|
|
|
| 40% |
Uncovered Elements: 3 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
|
| 143 |
3
|
@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 |
|
|
| 164 |
|
@see |
| 165 |
|
|
|
|
|
| 92.3% |
Uncovered Elements: 1 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
| 166 |
2
|
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 |
|
} |