-
Notifications
You must be signed in to change notification settings - Fork 835
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
150 additions
and
4 deletions.
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
49 changes: 49 additions & 0 deletions
49
engine/src/main/java/io/seldon/engine/config/MessageConvertersConfig.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,49 @@ | ||
package io.seldon.engine.config; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpInputMessage; | ||
import org.springframework.http.HttpOutputMessage; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.converter.AbstractHttpMessageConverter; | ||
import org.springframework.http.converter.HttpMessageConverter; | ||
import org.springframework.http.converter.HttpMessageNotReadableException; | ||
import org.springframework.http.converter.HttpMessageNotWritableException; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.List; | ||
|
||
import static io.seldon.engine.util.StreamUtils.copyStream; | ||
|
||
/** | ||
* Configure Spring Boot to allow upload of octet-stream. | ||
*/ | ||
@Configuration | ||
public class MessageConvertersConfig extends WebMvcConfigurationSupport { | ||
|
||
@Override | ||
protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) { | ||
converters.add(new AbstractHttpMessageConverter<InputStream>(MediaType.APPLICATION_OCTET_STREAM) { | ||
protected boolean supports(@NotNull Class<?> clazz) { | ||
return InputStream.class.isAssignableFrom(clazz); | ||
} | ||
|
||
@NotNull | ||
protected InputStream readInternal( | ||
@NotNull Class<? extends InputStream> clazz, | ||
@NotNull HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { | ||
return inputMessage.getBody(); | ||
} | ||
|
||
protected void writeInternal( | ||
@NotNull InputStream inputStream, | ||
@NotNull HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { | ||
copyStream(inputStream, outputMessage.getBody()); | ||
} | ||
}); | ||
|
||
super.configureMessageConverters(converters); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
engine/src/main/java/io/seldon/engine/util/StreamUtils.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 io.seldon.engine.util; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
public final class StreamUtils { | ||
private static final int EOF = -1; | ||
private static final int BUFFER_SIZE = 1024 * 4; | ||
|
||
public static byte[] toByteArray(InputStream inputStream) throws IOException { | ||
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
|
||
copyStream(inputStream, outputStream); | ||
|
||
return outputStream.toByteArray(); | ||
} | ||
|
||
public static void copyStream(final InputStream input, final OutputStream output) | ||
throws IOException { | ||
final byte[] buffer = new byte[BUFFER_SIZE]; | ||
int n; | ||
|
||
while (EOF != (n = input.read(buffer))) { | ||
output.write(buffer, 0, n); | ||
} | ||
} | ||
} |
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.