use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class ClientProxyImpl method handleMultipart.
private List<Attachment> handleMultipart(MultivaluedMap<ParameterType, Parameter> map, OperationResourceInfo ori, Object[] params) {
List<Attachment> atts = new LinkedList<Attachment>();
List<Parameter> fm = getParameters(map, ParameterType.REQUEST_BODY);
for (Parameter p : fm) {
Multipart part = getMultipart(ori, p.getIndex());
if (part != null) {
Object partObject = params[p.getIndex()];
if (partObject != null) {
atts.add(new Attachment(part.value(), part.type(), partObject));
}
}
}
return atts;
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class JwsMultipartSignatureInFilter method filter.
@Override
public void filter(List<Attachment> atts) {
if (atts.size() < 2) {
throw ExceptionUtils.toBadRequestException(null, null);
}
Attachment sigPart = atts.remove(atts.size() - 1);
String jwsSequence = null;
try {
jwsSequence = IOUtils.readStringFromStream(sigPart.getDataHandler().getInputStream());
} catch (IOException ex) {
throw ExceptionUtils.toBadRequestException(null, null);
}
String base64UrlEncodedHeaders = null;
String base64UrlEncodedSignature = null;
if (!useJwsJsonSignatureFormat) {
String[] parts = JoseUtils.getCompactParts(jwsSequence);
if (parts.length != 3 || parts[1].length() > 0) {
throw ExceptionUtils.toBadRequestException(null, null);
}
base64UrlEncodedHeaders = parts[0];
base64UrlEncodedSignature = parts[2];
} else {
Map<String, Object> parts = reader.fromJson(jwsSequence);
if (parts.size() != 2 || !parts.containsKey("protected") || !parts.containsKey("signature")) {
throw ExceptionUtils.toBadRequestException(null, null);
}
base64UrlEncodedHeaders = (String) parts.get("protected");
base64UrlEncodedSignature = (String) parts.get("signature");
}
JwsHeaders headers = new JwsHeaders(new JsonMapObjectReaderWriter().fromJson(JoseUtils.decodeToString(base64UrlEncodedHeaders)));
JoseUtils.traceHeaders(headers);
if (Boolean.FALSE != headers.getPayloadEncodingStatus()) {
throw ExceptionUtils.toBadRequestException(null, null);
}
JwsSignatureVerifier theVerifier = null;
if (verifier == null) {
Properties props = KeyManagementUtils.loadStoreProperties(message, true, JoseConstants.RSSEC_SIGNATURE_IN_PROPS, JoseConstants.RSSEC_SIGNATURE_PROPS);
theVerifier = JwsUtils.loadSignatureVerifier(message, props, headers);
} else {
theVerifier = verifier;
}
JwsVerificationSignature sig = theVerifier.createJwsVerificationSignature(headers);
if (sig == null) {
throw ExceptionUtils.toBadRequestException(null, null);
}
byte[] signatureBytes = JoseUtils.decode(base64UrlEncodedSignature);
byte[] headerBytesWithDot = StringUtils.toBytesASCII(base64UrlEncodedHeaders + ".");
sig.update(headerBytesWithDot, 0, headerBytesWithDot.length);
int attSize = atts.size();
for (int i = 0; i < attSize; i++) {
Attachment dataPart = atts.remove(i);
InputStream dataPartStream = null;
try {
dataPartStream = dataPart.getDataHandler().getDataSource().getInputStream();
} catch (IOException ex) {
throw ExceptionUtils.toBadRequestException(ex, null);
}
boolean verifyOnLastRead = i == attSize - 1 ? true : false;
JwsInputStream jwsStream = new JwsInputStream(dataPartStream, sig, signatureBytes, verifyOnLastRead);
InputStream newStream = null;
if (bufferPayload) {
CachedOutputStream cos = new CachedOutputStream();
try {
IOUtils.copy(jwsStream, cos);
newStream = cos.getInputStream();
} catch (Exception ex) {
throw ExceptionUtils.toBadRequestException(ex, null);
}
} else {
newStream = jwsStream;
}
Attachment newDataPart = new Attachment(newStream, dataPart.getHeaders());
atts.add(i, newDataPart);
}
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class FormUtils method populateMapFromMultipart.
public static void populateMapFromMultipart(MultivaluedMap<String, String> params, MultipartBody body, Message m, boolean decode) {
List<Attachment> atts = body.getAllAttachments();
checkNumberOfParts(m, atts.size());
for (Attachment a : atts) {
ContentDisposition cd = a.getContentDisposition();
if (cd != null && !MULTIPART_FORM_DATA_TYPE.equalsIgnoreCase(cd.getType())) {
continue;
}
String cdName = cd == null ? null : cd.getParameter("name");
String contentId = a.getContentId();
String name = StringUtils.isEmpty(cdName) ? contentId : cdName.replace("\"", "").replace("'", "");
if (StringUtils.isEmpty(name)) {
throw ExceptionUtils.toBadRequestException(null, null);
}
if (CONTENT_DISPOSITION_FILES_PARAM.equals(name)) {
// this is a reserved name in Content-Disposition for parts containing files
continue;
}
try {
String value = IOUtils.toString(a.getDataHandler().getInputStream());
params.add(HttpUtils.urlDecode(name), decode ? HttpUtils.urlDecode(value) : value);
} catch (IllegalArgumentException ex) {
LOG.warning("Illegal URL-encoded characters, make sure that no " + "@FormParam and @Multipart annotations are mixed up");
throw ExceptionUtils.toInternalServerErrorException(ex, null);
} catch (IOException ex) {
throw ExceptionUtils.toBadRequestException(null, null);
}
}
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class AttachmentUtils method fromListToMap.
private static Map<String, Attachment> fromListToMap(List<Attachment> atts, boolean preferContentDisposition) {
Map<String, Attachment> map = new LinkedHashMap<String, Attachment>();
for (Attachment a : atts) {
String contentId = null;
if (preferContentDisposition) {
ContentDisposition cd = a.getContentDisposition();
if (cd != null) {
contentId = cd.getParameter("name");
}
}
if (contentId == null) {
contentId = a.getContentId();
}
map.put(contentId, a);
}
return map;
}
use of org.apache.cxf.jaxrs.ext.multipart.Attachment in project cxf by apache.
the class AttachmentUtils method getMultipart.
public static Attachment getMultipart(Multipart id, MediaType mt, List<Attachment> infos) throws IOException {
if (id != null) {
for (Attachment a : infos) {
if (matchAttachmentId(a, id)) {
checkMediaTypes(a.getContentType(), id.type());
return a;
}
}
if (id.required()) {
org.apache.cxf.common.i18n.Message errorMsg = new org.apache.cxf.common.i18n.Message("MULTTIPART_ID_NOT_FOUND", BUNDLE, id.value(), mt.toString());
LOG.warning(errorMsg.toString());
throw ExceptionUtils.toBadRequestException(new MultipartReadException(id.value(), id.type(), errorMsg.toString()), null);
}
return null;
}
return !infos.isEmpty() ? infos.get(0) : null;
}
Aggregations