-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for multipart downloading of a translated file to F…
…iles API (#104) feat: add support for multipart downloading of a translated file to File API
- Loading branch information
1 parent
f3e4658
commit 1d08d6c
Showing
8 changed files
with
748 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...les-api/src/main/java/com/smartling/api/files/v2/resteasy/ext/TranslatedFileMetadata.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.smartling.api.files.v2.resteasy.ext; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class TranslatedFileMetadata | ||
{ | ||
// Now we leave the class empty and will use it as an end marker for the file body only. | ||
// In the future we will expand this object with additional metadata associated with the downloaded file. | ||
} |
29 changes: 29 additions & 0 deletions
29
...es-api/src/main/java/com/smartling/api/files/v2/resteasy/ext/TranslatedFileMultipart.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.smartling.api.files.v2.resteasy.ext; | ||
|
||
import lombok.Getter; | ||
import org.apache.commons.io.IOUtils; | ||
import org.jboss.resteasy.plugins.providers.multipart.MultipartInput; | ||
|
||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import java.io.InputStream; | ||
|
||
public class TranslatedFileMultipart | ||
{ | ||
@Getter | ||
InputStream fileBody; | ||
@Getter | ||
MultivaluedMap<String, String> fileHeaders; | ||
@Getter | ||
MediaType fileMediaType; | ||
@Getter | ||
TranslatedFileMetadata translatedFileMetadata; | ||
MultipartInput multipartInput; | ||
|
||
public void close() | ||
{ | ||
IOUtils.closeQuietly(fileBody); | ||
if (null != multipartInput) | ||
multipartInput.close(); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
.../src/main/java/com/smartling/api/files/v2/resteasy/ext/TranslatedFileMultipartReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.smartling.api.files.v2.resteasy.ext; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.jboss.resteasy.plugins.providers.multipart.InputPart; | ||
import org.jboss.resteasy.plugins.providers.multipart.MultipartInputImpl; | ||
import org.jboss.resteasy.plugins.providers.multipart.i18n.Messages; | ||
|
||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import javax.ws.rs.ext.MessageBodyReader; | ||
import javax.ws.rs.ext.Provider; | ||
import javax.ws.rs.ext.Providers; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
|
||
import static java.lang.String.format; | ||
import static org.jboss.resteasy.plugins.providers.multipart.MultipartConstants.MULTIPART_MIXED; | ||
|
||
@Provider | ||
@Consumes(MULTIPART_MIXED) | ||
public class TranslatedFileMultipartReader implements MessageBodyReader<TranslatedFileMultipart> | ||
{ | ||
private static final String BOUNDARY_PARAMETER = "boundary"; | ||
|
||
protected @Context Providers workers; | ||
|
||
@Override | ||
public boolean isReadable(Class<?> clazz, Type type, Annotation[] annotations, MediaType mediaType) | ||
{ | ||
return type.equals(TranslatedFileMultipart.class); | ||
} | ||
|
||
@Override | ||
public TranslatedFileMultipart readFrom(Class<TranslatedFileMultipart> clazz, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> multivaluedMap, InputStream inputStream) throws IOException, WebApplicationException | ||
{ | ||
final String boundary = mediaType.getParameters().get(BOUNDARY_PARAMETER); | ||
if (null == boundary) | ||
throw new IOException(Messages.MESSAGES.unableToGetBoundary()); | ||
final MultipartInputImpl multipartInput = new MultipartInputImpl(mediaType, workers); | ||
multipartInput.parse(inputStream); | ||
|
||
if (multipartInput.getParts().size() != 2) | ||
throw new IOException(format("The response contains unexpected number of parts (%d). Two parts are expected. The first one is file's body, the second one is metadata object.", multipartInput.getParts().size())); | ||
|
||
final InputPart filePart = multipartInput.getParts().get(0); | ||
final InputPart metadataPart = multipartInput.getParts().get(1); | ||
|
||
final TranslatedFileMultipart translatedFileMultipart = new TranslatedFileMultipart(); | ||
translatedFileMultipart.multipartInput = multipartInput; | ||
translatedFileMultipart.fileBody = filePart.getBody(InputStream.class, null); | ||
translatedFileMultipart.fileHeaders = filePart.getHeaders(); | ||
translatedFileMultipart.fileMediaType = filePart.getMediaType(); | ||
|
||
final MediaType metadataPartMediaType = metadataPart.getMediaType(); | ||
if (null == metadataPartMediaType) | ||
throw new IOException("Missing content type of metadata part."); | ||
|
||
if (!StringUtils.equalsIgnoreCase(MediaType.APPLICATION_JSON_TYPE.getType(), metadataPartMediaType.getType()) || !StringUtils.equalsIgnoreCase(MediaType.APPLICATION_JSON_TYPE.getSubtype(), metadataPartMediaType.getSubtype())) | ||
throw new IOException(format("Unexpected content type of metadata part (%s). Expected (%s) metadata part.", metadataPart.getMediaType(), MediaType.APPLICATION_JSON_TYPE)); | ||
|
||
translatedFileMultipart.translatedFileMetadata = metadataPart.getBody(TranslatedFileMetadata.class, TranslatedFileMetadata.class); | ||
if (null == translatedFileMultipart.translatedFileMetadata) | ||
throw new IOException("Cannot construct metadata json object."); | ||
|
||
return translatedFileMultipart; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.