-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HWKMETRICS-185] Port over the empty payload filter and body intercep…
…tor.
- Loading branch information
Stefan Negrea
committed
Aug 13, 2015
1 parent
438593b
commit 3bbe95f
Showing
3 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
...api-jaxrs-1.1/src/main/java/org/hawkular/metrics/api/jaxrs/filter/EmptyPayloadFilter.java
This file contains 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
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; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...s-1.1/src/main/java/org/hawkular/metrics/api/jaxrs/interceptor/EmptyPayloadException.java
This file contains 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
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 { | ||
} |
67 changes: 67 additions & 0 deletions
67
...1.1/src/main/java/org/hawkular/metrics/api/jaxrs/interceptor/EmptyPayloadInterceptor.java
This file contains 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
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; | ||
} | ||
} |