Java Code Examples for org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput#getParts()

The following examples show how to use org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput#getParts() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DriverUploadEndpoint.java    From syndesis with Apache License 2.0 6 votes vote down vote up
private static InputStream getBinaryArtifact(MultipartFormDataInput input) {
    if (input == null || input.getParts() == null || input.getParts().isEmpty()) {
        throw new IllegalArgumentException("Multipart request is empty");
    }

    try {
        final InputStream result = input.getFormDataPart("file", InputStream.class, null);

        if (result == null) {
            throw new IllegalArgumentException("Can't find a valid 'file' part in the multipart request");
        }

        return result;
    } catch (IOException e) {
        throw new IllegalArgumentException("Error while reading multipart request", e);
    }
}
 
Example 2
Source File: ExtensionHandler.java    From syndesis with Apache License 2.0 6 votes vote down vote up
@Nonnull
@SuppressWarnings("PMD.CyclomaticComplexity")
private static InputStream getBinaryArtifact(MultipartFormDataInput input) {
    if (input == null || input.getParts() == null || input.getParts().isEmpty()) {
        throw new IllegalArgumentException("Multipart request is empty");
    }

    try {
        InputStream result;
        if (input.getParts().size() == 1) {
            InputPart filePart = input.getParts().iterator().next();
            result = filePart.getBody(InputStream.class, null);
        } else {
            result = input.getFormDataPart("file", InputStream.class, null);
        }

        if (result == null) {
            throw new IllegalArgumentException("Can't find a valid 'file' part in the multipart request");
        }

        return result;
    } catch (IOException e) {
        throw new IllegalArgumentException("Error while reading multipart request", e);
    }
}
 
Example 3
Source File: DriverUploadEndpoint.java    From syndesis with Apache License 2.0 5 votes vote down vote up
private static String getFileName(MultipartFormDataInput input) {
    if (input == null || input.getParts() == null || input.getParts().isEmpty()) {
        throw new IllegalArgumentException("Multipart request is empty");
    }
    try {
        final String fileName = input.getFormDataPart("fileName", String.class, null);
        if (fileName == null) {
            throw new IllegalArgumentException("Can't find a valid 'fileName' part in the multipart request");
        }
        return fileName;
    } catch (IOException ex) {
        throw SyndesisServerException.launderThrowable("Unable to obtain fileName", ex);
    }
}
 
Example 4
Source File: ConnectorIconHandler.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@POST
@Operation(description = "Updates the connector icon for the specified connector and returns the updated connector")
@ApiResponse(responseCode = "200", description = "Updated Connector icon")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
@SuppressWarnings("PMD.CyclomaticComplexity")
public Connector create(MultipartFormDataInput dataInput) {
    if (dataInput == null || dataInput.getParts() == null || dataInput.getParts().isEmpty()) {
        throw new IllegalArgumentException("Multipart request is empty");
    }

    if (dataInput.getParts().size() != 1) {
        throw new IllegalArgumentException("Wrong number of parts in multipart request");
    }

    try {
        InputPart filePart = dataInput.getParts().iterator().next();
        try (InputStream result = filePart.getBody(InputStream.class, null)) {
            if (result == null) {
                throw new IllegalArgumentException("Can't find a valid 'icon' part in the multipart request");
            }

            try (BufferedInputStream iconStream = new BufferedInputStream(result)) {
                MediaType mediaType = filePart.getMediaType();
                if (!mediaType.getType().equals("image")) {
                    // URLConnection.guessContentTypeFromStream resets the stream after inspecting the media type so
                    // can continue to be used, rather than being consumed.
                    String guessedMediaType = URLConnection.guessContentTypeFromStream(iconStream);
                    if (!guessedMediaType.startsWith("image/")) {
                        throw new IllegalArgumentException("Invalid file contents for an image");
                    }
                    mediaType = MediaType.valueOf(guessedMediaType);
                }

                Icon.Builder iconBuilder = new Icon.Builder()
                                               .mediaType(mediaType.toString());

                Icon icon;
                String connectorIcon = connector.getIcon();
                if (connectorIcon != null && connectorIcon.startsWith("db:")) {
                    String connectorIconId = connectorIcon.substring(3);
                    iconBuilder.id(connectorIconId);
                    icon = iconBuilder.build();
                    getDataManager().update(icon);
                } else {
                    icon = getDataManager().create(iconBuilder.build());
                }
                //write icon to (Sql)FileStore
                iconDao.write(icon.getId().get(), iconStream);

                Connector updatedConnector = new Connector.Builder().createFrom(connector).icon("db:" + icon.getId().get()).build();
                getDataManager().update(updatedConnector);
                return updatedConnector;
            }
        }
    } catch (IOException e) {
        throw new IllegalArgumentException("Error while reading multipart request", e);
    }
}