Skip to content

Instantly share code, notes, and snippets.

@gedl
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gedl/5e359a7cdf8d6f83b099 to your computer and use it in GitHub Desktop.
Save gedl/5e359a7cdf8d6f83b099 to your computer and use it in GitHub Desktop.
##### REQUEST #####
curl -v -X POST -d "{}" http://localhost:8080/v1/session?preset=min --header "Content-Type:application/json"
* Hostname was NOT found in DNS cache
* Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> POST /v1/session?preset=min HTTP/1.1
> User-Agent: curl/7.37.0
> Host: localhost:8080
> Accept: */*
> Content-Type:application/json
> Content-Length: 2
>
* upload completely sent off: 2 out of 2 bytes
< HTTP/1.1 200 OK
< Date: Tue, 24 Jun 2014 15:01:15 GMT
< Content-Type: application/json; charset=utf-8
< Transfer-Encoding: chunked
<
{"id":"8m7ncd8z1sckqu1hm7dgl9atzoj0td4vv","authenticated":false,"memberId":4675863136}* Connection #0 to host localhost left intact
#### STACKTRACE #####
None in this case as at JacksonMessageBodyProvider.validate(Annotation[], Object) line: 69 the "value" parameter now has an instance of "SessionStandard" (see ResourceMethod) created with the default constructor.
##### REQUEST #####
curl -v -X POST -d "" http://localhost:8080/v1/session?preset=min --header "Content-Type:application/json"
* Hostname was NOT found in DNS cache
* Trying ::1...
* Connected to localhost (::1) port 8080 (#0)
> POST /v1/session?preset=min HTTP/1.1
> User-Agent: curl/7.37.0
> Host: localhost:8080
> Accept: */*
> Content-Type:application/json
> Content-Length: 0
>
< HTTP/1.1 422
< Date: Tue, 24 Jun 2014 14:55:45 GMT
< Content-Type: application/json; charset=utf-8
< Transfer-Encoding: chunked
* HTTP error before end of send, stop sending
<
{"errors":[]}* Closing connection 0
#### STACKTRACE ##### (exception does not get thrown because it's catched somewhere else but is of type ConstraintViolationException and the message is "The request entity was empty"
JacksonMessageBodyProvider.validate(Annotation[], Object) line: 69
JacksonMessageBodyProvider.readFrom(Class<Object>, Type, Annotation[], MediaType, MultivaluedMap<String,String>, InputStream) line: 60
ContainerRequest.getEntity(Class<T>, Type, Annotation[]) line: 490
EntityParamDispatchProvider$EntityInjectable.getValue(HttpContext) line: 123
InjectableValuesProvider.getInjectableValues(HttpContext) line: 86
AbstractResourceMethodDispatchProvider$TypeOutInvoker(AbstractResourceMethodDispatchProvider$EntityParamInInvoker).getParams(HttpContext) line: 153
AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(Object, HttpContext) line: 183
AbstractResourceMethodDispatchProvider$TypeOutInvoker(ResourceJavaMethodDispatcher).dispatch(Object, HttpContext) line: 75
InstrumentedResourceMethodDispatchProvider$MeteredRequestDispatcher.dispatch(Object, HttpContext) line: 49
OptionalResourceMethodDispatchAdapter$OptionalRequestDispatcher.dispatch(Object, HttpContext) line: 37
HttpMethodRule.accept(CharSequence, Object, UriRuleContext) line: 302
ResourceObjectRule.accept(CharSequence, Object, UriRuleContext) line: 100
RightHandPathRule.accept(CharSequence, Object, UriRuleContext) line: 147
RootResourceClassesRule.accept(CharSequence, Object, UriRuleContext) line: 84
WebApplicationImpl._handleRequest(WebApplicationContext, ContainerRequest) line: 1542
WebApplicationImpl._handleRequest(WebApplicationContext, ContainerRequest, ContainerResponse) line: 1473
WebApplicationImpl.handleRequest(ContainerRequest, ContainerResponse) line: 1419
WebApplicationImpl.handleRequest(ContainerRequest, ContainerResponseWriter) line: 1409
ServletContainer$InternalWebComponent(WebComponent).service(URI, URI, HttpServletRequest, HttpServletResponse) line: 409
ServletContainer.service(URI, URI, HttpServletRequest, HttpServletResponse) line: 540
ServletContainer.service(HttpServletRequest, HttpServletResponse) line: 715
ServletContainer(HttpServlet).service(ServletRequest, ServletResponse) line: 848
NonblockingServletHolder.handle(Request, ServletRequest, ServletResponse) line: 49
ServletHandler$CachedChain.doFilter(ServletRequest, ServletResponse) line: 1515
BiDiGzipFilter(UserAgentFilter).doFilter(ServletRequest, ServletResponse, FilterChain) line: 83
BiDiGzipFilter(GzipFilter).doFilter(ServletRequest, ServletResponse, FilterChain) line: 295
BiDiGzipFilter.doFilter(ServletRequest, ServletResponse, FilterChain) line: 127
ServletHandler$CachedChain.doFilter(ServletRequest, ServletResponse) line: 1486
ThreadNameFilter.doFilter(ServletRequest, ServletResponse, FilterChain) line: 29
ServletHandler$CachedChain.doFilter(ServletRequest, ServletResponse) line: 1486
AllowedMethodsFilter.handle(HttpServletRequest, HttpServletResponse, FilterChain) line: 44
AllowedMethodsFilter.doFilter(ServletRequest, ServletResponse, FilterChain) line: 39
ServletHandler$CachedChain.doFilter(ServletRequest, ServletResponse) line: 1486
ServletHandler.doHandle(String, Request, HttpServletRequest, HttpServletResponse) line: 519
MutableServletContextHandler(ContextHandler).doHandle(String, Request, HttpServletRequest, HttpServletResponse) line: 1097
ServletHandler.doScope(String, Request, HttpServletRequest, HttpServletResponse) line: 448
MutableServletContextHandler(ContextHandler).doScope(String, Request, HttpServletRequest, HttpServletResponse) line: 1031
MutableServletContextHandler(ScopedHandler).handle(String, Request, HttpServletRequest, HttpServletResponse) line: 136
InstrumentedHandler(HandlerWrapper).handle(String, Request, HttpServletRequest, HttpServletResponse) line: 97
InstrumentedHandler.handle(String, Request, HttpServletRequest, HttpServletResponse) line: 175
RoutingHandler.handle(String, Request, HttpServletRequest, HttpServletResponse) line: 51
RequestLogHandler(HandlerWrapper).handle(String, Request, HttpServletRequest, HttpServletResponse) line: 97
RequestLogHandler.handle(String, Request, HttpServletRequest, HttpServletResponse) line: 92
StatisticsHandler(HandlerWrapper).handle(String, Request, HttpServletRequest, HttpServletResponse) line: 97
StatisticsHandler.handle(String, Request, HttpServletRequest, HttpServletResponse) line: 162
Server(HandlerWrapper).handle(String, Request, HttpServletRequest, HttpServletResponse) line: 97
Server.handle(HttpChannel<?>) line: 446
HttpConnection$HttpChannelOverHttp(HttpChannel<T>).handle() line: 271
HttpConnection.onFillable() line: 246
AbstractConnection$ReadCallback.run() line: 358
InstrumentedQueuedThreadPool(QueuedThreadPool).runJob(Runnable) line: 601
QueuedThreadPool$3.run() line: 532
Thread.run() line: not available
@POST
@Metered
@ApiOperation(value = "Create a new session",
response = Session.class)
public Session createSession(
@ApiParam(value = "Session Details", required = false) SessionStandard session ,
@ApiParam(allowableValues = ApiFilterConstants.PRESET_OBJECT_TYPE_FILTER_MINIMAL_OBJECT + "," +
ApiFilterConstants.PRESET_OBJECT_TYPE_FILTER_STANDARD_OBJECT)
@DefaultValue(ApiFilterConstants.PRESET_OBJECT_TYPE_FILTER_MINIMAL_OBJECT)
@QueryParam(value = ApiFilterConstants.PRESET_OBJECT_TYPE_FILTER_NAME) String presetFieldType )
throws CommonException {/*...*/}
}
package x;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerRequestFilter;
@Provider
public class TransformNullPostAndPutBodiesToEmptyJsonObjectFilter implements ContainerRequestFilter {
private Logger logger = LoggerFactory.getLogger(TransformNullPostAndPutBodiesToEmptyJsonObjectFilter.class);
private final String EMPTY_JSON_OBJECT = "{}";
@Override
public ContainerRequest filter( ContainerRequest request ) {
MediaType mediaType = request.getMediaType();
String method = request.getMethod();
boolean isMethodPostOrPut = ("POST".equalsIgnoreCase(method) || "PUT".equalsIgnoreCase(method));
boolean isJSON = (MediaType.APPLICATION_JSON_TYPE.equals(mediaType));
try {
boolean isEntityEmpty = (request.getEntityInputStream().available() == 0);
if (isMethodPostOrPut && isJSON && isEntityEmpty) {
logger.info("Got an empty entity body with method={} and mediaType={}. Will inject {} into the request", method, mediaType,
EMPTY_JSON_OBJECT);
//close the old stream (we're not taking any chances)
if (request.getEntityInputStream() != null) {
request.getEntityInputStream().close();
}
//set the new one
InputStream is = new ByteArrayInputStream(EMPTY_JSON_OBJECT.getBytes());
request.setEntityInputStream(is);
logger.info("Successfully injected {} into the request", EMPTY_JSON_OBJECT);
}
} catch (IOException e) {
logger.error("Caught exception while trying to inject an empty JSON object into the request: {}\n.", e.getMessage());
logger.error("Will proceed with method={},mediaType={} and empty entity body. This may cause some problems in a second", method,
mediaType);
}
return request;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment