TCK: Resolves FormParam-related failures
Should fix:
allParamsInParamTest_from_standalone
allParamsOnFieldTest_from_standalone
formParamInParamTest_from_standalone
formParamOnFieldTest_from_standalone
package org.apache.cxf.jaxrs.utils;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -89,6 +91,7 @@ import org.apache.cxf.common.util.PropertyUtils;
import org.apache.cxf.common.util.ReflectionUtil;
import org.apache.cxf.common.util.StringUtils;
import org.apache.cxf.helpers.DOMUtils;
+import org.apache.cxf.helpers.IOUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.io.CacheAndWriteOutputStream;
import org.apache.cxf.io.ReaderInputStream;
@@ -863,14 +866,6 @@ public final class JAXRSUtils {
OperationResourceInfo ori)
throws IOException, WebApplicationException {
- InputStream is = message.getContent(InputStream.class);
- if (is == null) {
- Reader reader = message.getContent(Reader.class);
- if (reader != null) {
- is = new ReaderInputStream(reader);
- }
- }
-
if (parameterClass == AsyncResponse.class) {
return new AsyncResponseImpl(message);
}
@@ -882,6 +877,23 @@ public final class JAXRSUtils {
contentType = defaultCt == null ? MediaType.APPLICATION_OCTET_STREAM : defaultCt;
}
+ MessageContext mc = new MessageContextImpl(message);
+ MediaType mt = mc.getHttpHeaders().getMediaType();
+
+ InputStream is;
+ if (mt == null || mt.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE)) {
+ is = copyAndGetEntityStream(message);
+ } else {
+ is = message.getContent(InputStream.class);
+ }
+
+ if (is == null) {
+ Reader reader = message.getContent(Reader.class);
+ if (reader != null) {
+ is = new ReaderInputStream(reader);
+ }
+ }
+
return readFromMessageBody(parameterClass,
parameterType,
parameterAnns,
@@ -1018,8 +1030,9 @@ public final class JAXRSUtils {
m.put(FormUtils.FORM_PARAM_MAP, params);
if (mt == null || mt.isCompatible(MediaType.APPLICATION_FORM_URLENCODED_TYPE)) {
+ InputStream entityStream = copyAndGetEntityStream(m);
String enc = HttpUtils.getEncoding(mt, StandardCharsets.UTF_8.name());
- String body = FormUtils.readBody(m.getContent(InputStream.class), enc);
+ String body = FormUtils.readBody(entityStream, enc);
FormUtils.populateMapFromStringOrHttpRequest(params, m, body, enc, decode);
} else {
if ("multipart".equalsIgnoreCase(mt.getType())
@@ -1877,4 +1890,16 @@ public final class JAXRSUtils {
return errorMessage;
}
+ // copy the input stream so that it is not inadvertently closed
+ private static InputStream copyAndGetEntityStream(Message m) {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ try {
+ IOUtils.copy(m.getContent(InputStream.class), baos);
+ } catch (IOException e) {
+ throw ExceptionUtils.toInternalServerErrorException(e, null);
+ }
+ final byte[] copiedBytes = baos.toByteArray();
+ m.setContent(InputStream.class, new ByteArrayInputStream(copiedBytes));
+ return new ByteArrayInputStream(copiedBytes);
+ }
}
