Created
February 13, 2013 15:15
-
-
Save bugs84/4945280 to your computer and use it in GitHub Desktop.
InputStreamDataSourceWithHackForSchemaValidation
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
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