Skip to content

Instantly share code, notes, and snippets.

@loind89
Created November 24, 2015 17:58
Show Gist options
  • Select an option

  • Save loind89/692fad83db01cc05b263 to your computer and use it in GitHub Desktop.

Select an option

Save loind89/692fad83db01cc05b263 to your computer and use it in GitHub Desktop.
/**
* Created by loind on 11/25/2015.
* tested with application/* . not test with multi-part
*/
public class JerseyRawBodyRequestFilter implements ContainerRequestFilter {
private static final Logger logger = LoggerFactory.getLogger(RawBodyRequestFilter.class);
@Override
public void filter(ContainerRequestContext request) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = request.getEntityStream();
final StringBuilder b = new StringBuilder();
try {
if (in.available() > 0) {
ReaderWriter.writeTo(in, out);
byte[] requestEntity = out.toByteArray();
String rawBody = printEntity(b, requestEntity);
request.setProperty("request_body_raw",rawBody);
// logger.debug("Request Raw Body [{}]",rawBody );
request.setEntityStream(new ByteArrayInputStream(requestEntity));
}
// return request;
} catch (IOException ex) {
throw new ContainerException(ex);
}
}
private String printEntity(StringBuilder b, byte[] entity) throws IOException {
if (entity.length == 0)
return null;
b.append(new String(entity)).append("\n");
return b.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment