Skip to content

Commit

Permalink
[HWKMETRICS-185] Copy but not port the filters from JAX-RS 2.0 implem…
Browse files Browse the repository at this point in the history
…entation.
  • Loading branch information
Stefan Negrea committed Jul 27, 2015
1 parent 2f9597c commit 521e21f
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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 java.io.IOException;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;

/**
* @author Stefan Negrea
*
*/
@Provider
public class CorsFilter implements ContainerResponseFilter {

public static final String DEFAULT_ALLOWED_METHODS = "GET, POST, PUT, DELETE, OPTIONS, HEAD";
public static final String DEFAULT_ALLOWED_HEADERS = "origin,accept,content-type,hawkular-tenant";

@Override
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) throws IOException {
final MultivaluedMap<String, Object> headers = responseContext
.getHeaders();

String origin = "*";
if (headers.get("origin") != null && headers.get("origin").size() == 1
&& headers.get("origin").get(0) != null
&& !headers.get("origin").get(0).equals("null")) {
origin = headers.get("origin").get(0).toString();
}
headers.add("Access-Control-Allow-Origin", origin);

headers.add("Access-Control-Allow-Credentials", "true");
headers.add("Access-Control-Allow-Methods", DEFAULT_ALLOWED_METHODS);
headers.add("Access-Control-Max-Age", 72 * 60 * 60);
headers.add("Access-Control-Allow-Headers", DEFAULT_ALLOWED_HEADERS);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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 java.io.IOException;

import javax.ws.rs.HttpMethod;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.Provider;

/**
* 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
*/
@Provider
public class EmptyPayloadFilter implements ContainerRequestFilter {
public static final String EMPTY_PAYLOAD = EmptyPayloadFilter.class.getName();

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
if (!HttpMethod.POST.equals(requestContext.getMethod())) {
return;
}
UriInfo uriInfo = requestContext.getUriInfo();
String path = uriInfo.getPath();
if (path.startsWith("/db")) {
// Skip some endpoints:
// - Influx
return;
}
requestContext.setProperty(EMPTY_PAYLOAD, Boolean.TRUE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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 static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;

import java.io.IOException;

import javax.inject.Inject;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.Provider;

import org.hawkular.metrics.api.jaxrs.ApiError;
import org.hawkular.metrics.api.jaxrs.MetricsServiceLifecycle;
import org.hawkular.metrics.api.jaxrs.MetricsServiceLifecycle.State;
import org.hawkular.metrics.api.jaxrs.handler.StatusHandler;

/**
* @author Matt Wringe
*/
@Provider
public class MetricsServiceStateFilter implements ContainerRequestFilter {

private static final String STARTING = "Service unavailable while initializing.";
private static final String FAILED = "Internal server error.";
private static final String STOPPED = "The service is no longer running.";

@Inject
private MetricsServiceLifecycle metricsServiceLifecycle;

@Override
public void filter(ContainerRequestContext containerRequestContext) throws IOException {
UriInfo uriInfo = containerRequestContext.getUriInfo();
String path = uriInfo.getPath();

if (path.startsWith(StatusHandler.PATH)) {
// The status page does not require the MetricsService to be up and running.
return;
}

if (metricsServiceLifecycle.getState() == State.STARTING) {
// Fail since the Cassandra cluster is not yet up yet.
Response response = Response.status(Status.SERVICE_UNAVAILABLE)
.type(APPLICATION_JSON_TYPE)
.entity(new ApiError(STARTING))
.build();
containerRequestContext.abortWith(response);
} else if (metricsServiceLifecycle.getState() == State.FAILED) {
// Fail since an error has occured trying to start the Metrics service
Response response = Response.status(Status.INTERNAL_SERVER_ERROR)
.type(APPLICATION_JSON_TYPE)
.entity(new ApiError(FAILED))
.build();
containerRequestContext.abortWith(response);
} else if (metricsServiceLifecycle.getState() == State.STOPPED ||
metricsServiceLifecycle.getState() == State.STOPPING) {
Response response = Response.status(Status.SERVICE_UNAVAILABLE)
.type(APPLICATION_JSON_TYPE)
.entity(new ApiError(STOPPED))
.build();
containerRequestContext.abortWith(response);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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 static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE;

import java.io.IOException;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.ext.Provider;

import org.hawkular.metrics.api.jaxrs.ApiError;
import org.hawkular.metrics.api.jaxrs.handler.BaseHandler;
import org.hawkular.metrics.api.jaxrs.handler.StatusHandler;

/**
* @author Stefan Negrea
*/
@Provider
public class TenantFilter implements ContainerRequestFilter {
public static final String TENANT_HEADER_NAME = "Hawkular-Tenant";

private static final String MISSING_TENANT_MSG;

static {
MISSING_TENANT_MSG = "Tenant is not specified. Use '"
+ TENANT_HEADER_NAME
+ "' header.";
}

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
UriInfo uriInfo = requestContext.getUriInfo();
String path = uriInfo.getPath();

if (path.startsWith("/tenants") || path.startsWith("/db") || path.startsWith(StatusHandler.PATH)
|| path.equals(BaseHandler.PATH)) {
// Tenants, Influx and status handlers do not check the tenant header
return;
}

String tenant = requestContext.getHeaders().getFirst(TENANT_HEADER_NAME);
if (tenant != null && !tenant.trim().isEmpty()) {
// We're good already
return;
}

// Fail on missing tenant info
Response response = Response.status(Status.BAD_REQUEST)
.type(APPLICATION_JSON_TYPE)
.entity(new ApiError(MISSING_TENANT_MSG))
.build();
requestContext.abortWith(response);
}
}

0 comments on commit 521e21f

Please sign in to comment.