Skip to content

Commit

Permalink
[HWKMETRICS-185] Port over the empty payload filter and body intercep…
Browse files Browse the repository at this point in the history
…tor.
  • Loading branch information
Stefan Negrea committed Aug 13, 2015
1 parent 438593b commit 3bbe95f
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.hawkular.metrics.api.jaxrs.filter;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.HttpMethod;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.core.ResourceMethod;
import org.jboss.resteasy.core.ServerResponse;
import org.jboss.resteasy.spi.Failure;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.interception.PreProcessInterceptor;

/**
* Set the {@link org.hawkular.metrics.api.jaxrs.filter.EmptyPayloadFilter#EMPTY_PAYLOAD} context property to
* {@link Boolean#TRUE} if the request is a POST.
*
* @author Thomas Segismont
* @author Stefan Negrea
*/
@Provider
public class EmptyPayloadFilter implements PreProcessInterceptor {
public static final String EMPTY_PAYLOAD = EmptyPayloadFilter.class.getName();

@Context
HttpServletRequest servletRequest;

@Override
public ServerResponse preProcess(HttpRequest request,
ResourceMethod resourceMethod) throws Failure,
WebApplicationException {

if (!HttpMethod.POST.equals(resourceMethod.getMethod())) {
return null;
}

servletRequest.getServletContext().setAttribute(EMPTY_PAYLOAD, Boolean.TRUE);
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.hawkular.metrics.api.jaxrs.interceptor;

/**
* Thrown by {@link EmptyPayloadInterceptor} when POST request body is empty.
*
* @author Thomas Segismont
*/
public class EmptyPayloadException extends RuntimeException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2014-2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.hawkular.metrics.api.jaxrs.interceptor;

import static java.lang.Boolean.TRUE;
import static org.hawkular.metrics.api.jaxrs.filter.EmptyPayloadFilter.EMPTY_PAYLOAD;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;

import javax.ws.rs.WebApplicationException;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.annotations.interception.ServerInterceptor;
import org.jboss.resteasy.spi.interception.MessageBodyReaderContext;
import org.jboss.resteasy.spi.interception.MessageBodyReaderInterceptor;

/**
* Make sure body is present if the {@link org.hawkular.metrics.api.jaxrs.filter.EmptyPayloadFilter#EMPTY_PAYLOAD}
* context property is set to {@link Boolean#TRUE}.
*
* @author Thomas Segismont
* @author Stefan Negrea
* @see org.hawkular.metrics.api.jaxrs.filter.EmptyPayloadFilter
*/
@Provider
@ServerInterceptor
public class EmptyPayloadInterceptor implements MessageBodyReaderInterceptor {

@Override
public Object read(MessageBodyReaderContext context) throws IOException, WebApplicationException {
Object object = context.proceed();
if (context.getAttribute(EMPTY_PAYLOAD) != TRUE) {
return object;
}
if (object instanceof Collection) {
Collection collection = (Collection) object;
if (collection.isEmpty()) {
throw new EmptyPayloadException();
}
} else if (object instanceof Map) {
Map map = (Map) object;
if (map.isEmpty()) {
throw new EmptyPayloadException();
}
} else if (object == null) {
throw new EmptyPayloadException();
}
return object;
}
}

0 comments on commit 3bbe95f

Please sign in to comment.