forked from phaneesh/revolver
-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resource method blacklist #23
Open
himadrisj
wants to merge
10
commits into
revolver-callback
Choose a base branch
from
resource_method_blacklist
base: revolver-callback
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
046ad2d
Add back CallbackResource from multi-module branch
himadrisj b1f6a29
Register Callback resource
himadrisj d9a0e44
Resource method blacklist processor
himadrisj cbb4a38
Add missing break statement
himadrisj 88721bd
Fix wrong condition
himadrisj afc7e7e
Add RevolverRequestResource class from master branch
himadrisj 7ddb108
Register RevolverRequestResource
himadrisj 066193b
refactors code
bb7ef0f
add comment
0a8587d
Merge pull request #24 from jitendradhawan/resource_method_blacklist_…
himadrisj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
14 changes: 14 additions & 0 deletions
14
src/main/java/io/dropwizard/revolver/provider/BlacklistMethodData.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,14 @@ | ||
package io.dropwizard.revolver.provider; | ||
|
||
import lombok.*; | ||
|
||
@Getter | ||
@Builder | ||
@ToString | ||
@AllArgsConstructor | ||
@EqualsAndHashCode | ||
public class BlacklistMethodData { | ||
private final String httpMethod; | ||
private final String relativePath; // path of the method excluding parent resource path | ||
private final String resourceClassName; | ||
} |
79 changes: 79 additions & 0 deletions
79
src/main/java/io/dropwizard/revolver/provider/BlacklistProcessor.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,79 @@ | ||
package io.dropwizard.revolver.provider; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.glassfish.jersey.server.model.ModelProcessor; | ||
import org.glassfish.jersey.server.model.Resource; | ||
import org.glassfish.jersey.server.model.Resource.Builder; | ||
import org.glassfish.jersey.server.model.ResourceMethod; | ||
import org.glassfish.jersey.server.model.ResourceModel; | ||
|
||
import javax.ws.rs.core.Configuration; | ||
import javax.ws.rs.ext.Provider; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
@Provider | ||
@Slf4j | ||
public class BlacklistProcessor implements ModelProcessor { | ||
|
||
private final Set<BlacklistMethodData> blacklistMethods; | ||
|
||
public BlacklistProcessor(final Set<BlacklistMethodData> blacklistMethods) { | ||
this.blacklistMethods = blacklistMethods; | ||
} | ||
|
||
@Override | ||
public ResourceModel processResourceModel(final ResourceModel resourceModel, final Configuration configuration) { | ||
if (blacklistMethods == null || blacklistMethods.isEmpty()) { | ||
log.info("No API end-point to blacklist"); | ||
return resourceModel; | ||
} | ||
|
||
ResourceModel.Builder newResourceModelBuilder = new ResourceModel.Builder(false); | ||
for (final Resource resource : resourceModel.getResources()) { | ||
final Resource.Builder resourceBuilder; | ||
|
||
final List<Resource> childResources = resource.getChildResources(); | ||
|
||
resourceBuilder = childResources.isEmpty() | ||
? Resource.builder(resource) | ||
: Resource.builder(resource.getPath()); | ||
|
||
// Add child resources with non-blacklisted child resource methods only | ||
childResources.forEach(childResource -> { | ||
final Builder childResourceBuilder = Resource.builder(childResource.getPath()); | ||
childResource.getResourceMethods().stream() | ||
.filter(this::shouldAddMethod) | ||
.forEach(childResourceBuilder::addMethod); | ||
resourceBuilder.addChildResource(childResourceBuilder.build()); | ||
}); | ||
|
||
newResourceModelBuilder.addResource(resourceBuilder.build()); | ||
} | ||
|
||
return newResourceModelBuilder.build(); | ||
} | ||
|
||
@Override | ||
public ResourceModel processSubResource(final ResourceModel subResourceModel, final Configuration configuration) { | ||
return subResourceModel; | ||
} | ||
|
||
private boolean shouldAddMethod(final ResourceMethod method) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably use as a predicate for filter to check if resource method is blacklisted |
||
// This is slightly sub-optimal, | ||
// but given that blacklist count will be small, not creating a complex Map to match Jersey resource structure | ||
return blacklistMethods.stream() | ||
.noneMatch(blacklistData -> { | ||
if (method.getInvocable().getHandler().getHandlerClass().getName().equals(blacklistData.getResourceClassName()) && | ||
method.getHttpMethod().equalsIgnoreCase(blacklistData.getHttpMethod()) && | ||
method.getParent().getPath().equals(blacklistData.getRelativePath())) { | ||
log.info("Blacklisting method with path: {}, http method: {}, resource: {}", | ||
blacklistData.getRelativePath(), | ||
blacklistData.getHttpMethod(), | ||
blacklistData.getResourceClassName()); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
src/main/java/io/dropwizard/revolver/resource/RevolverCallbackResource.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,105 @@ | ||
/* | ||
* Copyright 2016 Phaneesh Nagaraja <[email protected]>. | ||
* | ||
* 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 io.dropwizard.revolver.resource; | ||
|
||
import com.codahale.metrics.annotation.Metered; | ||
import com.google.common.base.Strings; | ||
import io.dropwizard.msgpack.MsgPackMediaType; | ||
import io.dropwizard.revolver.base.core.RevolverCallbackResponse; | ||
import io.dropwizard.revolver.callback.InlineCallbackHandler; | ||
import io.dropwizard.revolver.http.RevolverHttpCommand; | ||
import io.dropwizard.revolver.persistence.PersistenceProvider; | ||
import io.dropwizard.revolver.util.HeaderUtil; | ||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import javax.inject.Singleton; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.HeaderParam; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.Context; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import lombok.extern.slf4j.Slf4j; | ||
import lombok.val; | ||
|
||
/** | ||
* @author phaneesh | ||
*/ | ||
@Path("/revolver") | ||
@Slf4j | ||
@Singleton | ||
@Api(value = "RequestCallback", description = "Revolver gateway api for callbacks on mailbox requests") | ||
public class RevolverCallbackResource { | ||
|
||
private static final String RESPONSE_CODE_HEADER = "X-RESPONSE-CODE"; | ||
|
||
private final PersistenceProvider persistenceProvider; | ||
|
||
private final InlineCallbackHandler callbackHandler; | ||
|
||
public RevolverCallbackResource(PersistenceProvider persistenceProvider, | ||
InlineCallbackHandler callbackHandler) { | ||
this.persistenceProvider = persistenceProvider; | ||
this.callbackHandler = callbackHandler; | ||
} | ||
|
||
@Path("/v1/callback/{requestId}") | ||
@POST | ||
@Metered | ||
@ApiOperation(value = "Callback for updating responses for a given mailbox request") | ||
@Produces({MediaType.APPLICATION_JSON, MsgPackMediaType.APPLICATION_MSGPACK, | ||
MediaType.APPLICATION_XML}) | ||
@Consumes({MediaType.APPLICATION_JSON, MsgPackMediaType.APPLICATION_MSGPACK, | ||
MediaType.APPLICATION_XML}) | ||
public Response handleCallback(@PathParam("requestId") String requestId, | ||
@HeaderParam(RESPONSE_CODE_HEADER) String responseCode, | ||
@Context HttpHeaders headers, byte[] responseBody) { | ||
long start = System.currentTimeMillis(); | ||
try { | ||
val callbackRequest = persistenceProvider.request(requestId); | ||
log.debug("Callback request in handleCallback : " + callbackRequest); | ||
if (callbackRequest == null) { | ||
return Response.status(Response.Status.BAD_REQUEST).build(); | ||
} | ||
val response = RevolverCallbackResponse.builder().body(responseBody) | ||
.headers(headers.getRequestHeaders()).statusCode( | ||
responseCode != null ? Integer.parseInt(responseCode) | ||
: Response.Status.OK.getStatusCode()).build(); | ||
val mailboxTtl = HeaderUtil.getTTL(callbackRequest); | ||
persistenceProvider.saveResponse(requestId, response, mailboxTtl); | ||
if (callbackRequest.getMode() != null && ( | ||
callbackRequest.getMode().equals(RevolverHttpCommand.CALL_MODE_CALLBACK) | ||
|| callbackRequest.getMode() | ||
.equals(RevolverHttpCommand.CALL_MODE_CALLBACK_SYNC)) && !Strings | ||
.isNullOrEmpty(callbackRequest.getCallbackUri())) { | ||
callbackHandler.handle(requestId, response); | ||
} | ||
log.info( | ||
"Callback processing for request id: {} with response size: {} bytes completed in {} ms", | ||
requestId, responseBody.length, (System.currentTimeMillis() - start)); | ||
return Response.accepted().build(); | ||
} catch (Exception e) { | ||
log.error("Callback error", e); | ||
return Response.serverError().build(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
refactor to reduce cyclomatic complexity for the nested for and if branches. probably use java 8 streams and collectors with filters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Streams would be difficult to use here.