Created
November 24, 2015 17:58
-
-
Save loind89/692fad83db01cc05b263 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * 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