Search in sources :

Example 51 with Attachment

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;
}
Also used : Multipart(org.apache.cxf.jaxrs.ext.multipart.Multipart) Parameter(org.apache.cxf.jaxrs.model.Parameter) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) LinkedList(java.util.LinkedList)

Example 52 with Attachment

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);
    }
}
Also used : JwsInputStream(org.apache.cxf.rs.security.jose.jws.JwsInputStream) InputStream(java.io.InputStream) JsonMapObjectReaderWriter(org.apache.cxf.jaxrs.json.basic.JsonMapObjectReaderWriter) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) IOException(java.io.IOException) Properties(java.util.Properties) IOException(java.io.IOException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) JwsSignatureVerifier(org.apache.cxf.rs.security.jose.jws.JwsSignatureVerifier) JwsHeaders(org.apache.cxf.rs.security.jose.jws.JwsHeaders) JwsVerificationSignature(org.apache.cxf.rs.security.jose.jws.JwsVerificationSignature) JwsInputStream(org.apache.cxf.rs.security.jose.jws.JwsInputStream)

Example 53 with Attachment

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);
        }
    }
}
Also used : ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) IOException(java.io.IOException)

Example 54 with Attachment

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;
}
Also used : ContentDisposition(org.apache.cxf.jaxrs.ext.multipart.ContentDisposition) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment) LinkedHashMap(java.util.LinkedHashMap)

Example 55 with Attachment

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;
}
Also used : Message(org.apache.cxf.message.Message) Attachment(org.apache.cxf.jaxrs.ext.multipart.Attachment)

Aggregations

Attachment (org.apache.cxf.jaxrs.ext.multipart.Attachment)56 Test (org.junit.Test)20 InputStream (java.io.InputStream)19 Response (javax.ws.rs.core.Response)17 ArrayList (java.util.ArrayList)16 ByteArrayInputStream (java.io.ByteArrayInputStream)15 MultipartBody (org.apache.cxf.jaxrs.ext.multipart.MultipartBody)15 WebClient (org.apache.cxf.jaxrs.client.WebClient)14 ContentDisposition (org.apache.cxf.jaxrs.ext.multipart.ContentDisposition)14 IOException (java.io.IOException)10 PushbackInputStream (java.io.PushbackInputStream)8 POST (javax.ws.rs.POST)8 LinkedList (java.util.LinkedList)7 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)7 LinkedHashMap (java.util.LinkedHashMap)6 DataHandler (javax.activation.DataHandler)6 Path (javax.ws.rs.Path)6 QueryResponse (ddf.catalog.operation.QueryResponse)5 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)5 Consumes (javax.ws.rs.Consumes)5