Skip to content

Instantly share code, notes, and snippets.

@azare
Created July 24, 2015 15:33
Show Gist options
  • Save azare/f48c596e3a33e5e69c0a to your computer and use it in GitHub Desktop.
Save azare/f48c596e3a33e5e69c0a to your computer and use it in GitHub Desktop.
Incoming interceptor that reads the HTTP request headers
public class RequestHeaderInterceptor extends AbstractPhaseInterceptor<Message> {
private final static String HEADER_STATUS_CODE_OVERRIDE_KEY = "X-HTTP-Status-Code-Override";
public AuthenticationInterceptor() {
super(Phase.READ);
}
@SuppressWarnings("unchecked")
@Override
public void handleMessage(Message message) throws Fault {
final List<String> overrideHttpStatusCode = ((Map<String, List<String>>) message.get(Message.PROTOCOL_HEADERS)).get(HEADER_STATUS_CODE_OVERRIDE_KEY);
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(overrideHttpStatusCode)) {
try {
ServiceContext.get().setHttpStatusCodeValue(NumberUtils.toInt(overrideHttpStatusCode.get(0)));
}
catch (Exception ex) {
ResponseBuilder rb = Response.fromResponse(new WebApplicationException(ex, 400).getResponse());
String acceptTypes = HttpUtils.getProtocolHeader(message, Message.ACCEPT_CONTENT_TYPE, null);
if (acceptTypes == null) {
acceptTypes = "*/*";
}
List<MediaType> acceptContentTypes = JAXRSUtils.sortMediaTypes(acceptTypes);
if (acceptContentTypes.get(0).toString().equals(MediaType.APPLICATION_XML)) {
rb.type(MediaType.APPLICATION_XML_TYPE);
}
else {
rb.type(MediaType.APPLICATION_JSON_TYPE);
}
throw new WebApplicationException(rb.build());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment