Skip to content

Instantly share code, notes, and snippets.

@bugs84
Created February 13, 2013 15:15
Show Gist options
  • Save bugs84/4945280 to your computer and use it in GitHub Desktop.
Save bugs84/4945280 to your computer and use it in GitHub Desktop.
InputStreamDataSourceWithHackForSchemaValidation
import javax.activation.DataSource;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Vzniklo to, protoze servisa, ktera ma zapnute @SchemaValidation
* Udela to, ze pri validaci zavola getInputStream a cely ho precte
* Potom se pri vyplnovani responsu znova zavola getInputStream a chce ho napsat do response, jenomze
* to umre, protoze ho uz precetla a zavrela validace.
*
* Reseni: na prvni volani to nevrati pravy inputStream
*/
public class InputStreamDataSourceWithHackForSchemaValidation implements DataSource {
private InputStream inputStream;
private int count = 0;
public InputStreamDataSourceWithHackForSchemaValidation(InputStream inputStream) {
this.inputStream = inputStream;
}
@Override
public InputStream getInputStream() throws IOException {
if (count == 0) {
//First access to data stream is for @SchemaValidation (and we do not need read stream for validation)
count++;
return new ByteArrayInputStream(new byte[]{});
}
if (count != 1) {
throw new IllegalStateException("getInputStream was expected to be called exactly twice");
}
//second access is for writing data to response
count++;
return inputStream;
}
@Override
public OutputStream getOutputStream() throws IOException {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public String getContentType() {
return "*/*";
}
@Override
public String getName() {
return "InputStreamDataSource";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment