-
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.
- Loading branch information
1 parent
4549c5f
commit e176566
Showing
2 changed files
with
75 additions
and
51 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
api/metrics-api-jaxrs/src/main/java/org/hawkular/metrics/api/jaxrs/filter/TenantFilter.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,75 @@ | ||
/* | ||
* 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.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; | ||
|
||
/** | ||
* @author Stefan Negrea | ||
*/ | ||
@Provider | ||
public class TenantFilter implements ContainerRequestFilter { | ||
private static final String TENANT_HEADER_NAME = "tenantId"; | ||
private static final String TENANT_QUERY_PARAM_NAME = "tenantId"; | ||
private static final String MISSING_TENANT_MSG; | ||
|
||
static { | ||
MISSING_TENANT_MSG = "Tenant is not specified. Use '" | ||
+ TENANT_HEADER_NAME | ||
+ "' header or '" | ||
+ TENANT_QUERY_PARAM_NAME | ||
+ "' query parameter"; | ||
} | ||
|
||
@Override | ||
public void filter(ContainerRequestContext requestContext) throws IOException { | ||
UriInfo uriInfo = requestContext.getUriInfo(); | ||
String path = uriInfo.getPath(); | ||
|
||
if (path.startsWith("/tenants") || path.startsWith("/db")) { | ||
// Tenants and Influx 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; | ||
} | ||
|
||
tenant = uriInfo.getQueryParameters().getFirst(TENANT_QUERY_PARAM_NAME); | ||
if (tenant != null && !tenant.trim().isEmpty()) { | ||
// Move tenant info from query param to header | ||
// This makes the handler implementation easier | ||
requestContext.getHeaders().add(TENANT_HEADER_NAME, tenant); | ||
return; | ||
} | ||
|
||
// Fail on missing tenant info | ||
Response response = Response.status(Status.BAD_REQUEST).entity(new ApiError(MISSING_TENANT_MSG)).build(); | ||
requestContext.abortWith(response); | ||
} | ||
} |
51 changes: 0 additions & 51 deletions
51
...metrics-api-jaxrs/src/main/java/org/hawkular/metrics/api/jaxrs/filter/TenantIdFilter.java
This file was deleted.
Oops, something went wrong.