Advertisement
spkenny

Chain of responsibility

Feb 18th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.51 KB | None | 0 0
  1. /**
  2.  * It represents a handler and has two methods: one for handling requests and other for combining handlers
  3.  */
  4. @FunctionalInterface
  5. interface RequestHandler {
  6.  
  7.     // !!! write a method handle that accept request and returns new request here
  8.     // it allows to use lambda expressions for creating handlers below
  9.    
  10.     Request handle(Request request);
  11.  
  12.     // !!! write a default method for combining this and other handler single one
  13.     // the order of execution may be any but you need to consider it when composing handlers
  14.     // the method may has any name
  15.     default Request commonRequestHandler() {
  16.         return null;
  17.     }
  18. }
  19.  
  20. /**
  21.  * Accepts a request and returns new request with data wrapped in the tag <transaction>...</transaction>
  22.  */
  23. final static RequestHandler wrapInTransactionTag =
  24.         (req) -> new Request(String.format("<transaction>%s</transaction>", req.getData()));
  25.  
  26. /**
  27.  * Accepts a request and returns a new request with calculated digest inside the tag <digest>...</digest>
  28.  */
  29. final static RequestHandler createDigest =
  30.         (req) -> {
  31.             String digest = "";
  32.             try {
  33.                 final MessageDigest md5 = MessageDigest.getInstance("MD5");
  34.                 final byte[] digestBytes = md5.digest(req.getData().getBytes("UTF-8"));
  35.                 digest = new String(Base64.getEncoder().encode(digestBytes));
  36.             } catch (Exception ignored) { }
  37.             return new Request(req.getData() + String.format("<digest>%s</digest>", digest));
  38.         };
  39.  
  40. /**
  41.  * Accepts a request and returns a new request with data wrapped in the tag <request>...</request>
  42.  */
  43. final static RequestHandler wrapInRequestTag =
  44.         (req) -> new Request(String.format("<request>%s</request>", req.getData()));
  45.  
  46. /**
  47.  * It should represents a chain of responsibility combined from another handlers.
  48.  * The format: commonRequestHandler = handler1.setSuccessor(handler2.setSuccessor(...))
  49.  * The combining method setSuccessor may has another name
  50.  */
  51. final static RequestHandler commonRequestHandler = (req) -> wrapInRequestTag.handle(createDigest.handle(wrapInTransactionTag.handle(req))); // !!! write the combining of existing handlers here
  52.  
  53. /**
  54.  * Immutable class for representing requests.
  55.  * If you need to change the request data then create new request.
  56.  */
  57. static class Request {
  58.     private final String data;
  59.  
  60.     public Request(String requestData) {
  61.         this.data = requestData;
  62.     }
  63.  
  64.     public String getData() {
  65.         return data;
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement