Skip to content

Commit

Permalink
AUTOPAY-26733: Add Servlet-friendly methods to http-signatures library (
Browse files Browse the repository at this point in the history
#66)

* AUTOPAY-26733: Add Servlet-friendly methods to http-signatures library

* AUTOPAY-26733: Add Servlet-friendly methods to http-signatures library

* AUTOPAY-26733: Add Servlet-friendly methods to http-signatures library

License header updated

* AUTOPAY-26733: Add Servlet-friendly methods to http-signatures library

JavaDoc and Readme updated

---------

Co-authored-by: Robert Borys <[email protected]>
  • Loading branch information
branislavbednar and robelcik authored Jan 9, 2024
1 parent 47d54b5 commit 8c86de7
Show file tree
Hide file tree
Showing 82 changed files with 283 additions and 81 deletions.
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022-2023 Visma Autopay AS
Copyright (c) 2022-2024 Visma Autopay AS

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ var requestContext = SignatureContext.builder()
.method("POST")
// Can be provided as a String or URI
.targetUri("https://example.com/foo")
// Or from HttpServletRequest object
.targetUri(request.getRequestURL(), request.getQueryString())
// Status for response signature
.status(200)

Expand All @@ -264,6 +266,10 @@ var requestContext = SignatureContext.builder()
.headers(Map.of("Header-One", "valueOne", "Header-Two", "valueTwo"))
// Or as a "MultivaluedMap", often used in frameworks
.headers(Map.of("Header-One", List.of("valueOne", "valueTwo")))
// Or from HttpServletRequest object
.headers(request.getHeaderNames(), request::getHeaders)
// Or from HttpServletResponse object
.headers(response.getHeaderNames(), response::getHeaders)

// Context of related request if used in response signature
.relatedRequest(relatedRequestContext)
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2022-2023 Visma Autopay AS
Copyright (c) 2022-2024 Visma Autopay AS
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
101 changes: 100 additions & 1 deletion src/main/java/net/visma/autopay/http/signature/SignatureContext.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -23,10 +23,13 @@

import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;


Expand Down Expand Up @@ -162,6 +165,8 @@ public Builder method(String method) {

/**
* Sets target URI
* <p>
* {@code targetUri} is not the same as servlet's {@code requestUri}
*
* @param targetUri Target URI given as {@link URI} object
* @return This builder
Expand All @@ -173,6 +178,8 @@ public Builder targetUri(URI targetUri) {

/**
* Sets target URI
* <p>
* {@code targetUri} is not the same as servlet's {@code requestUri}.
*
* @param targetUri Target URI given as {@link String}
* @return This builder
Expand All @@ -182,6 +189,38 @@ public Builder targetUri(String targetUri) {
return this;
}

/**
* Sets target URI
* <p>
* Method offers compatibility with {@code HttpServletRequest} methods.
* {@code targetUri} is not the same as servlet's {@code requestUri}.
* <p>
* Example:
* <pre>
* SignatureContext.builder()
* ...
* .targetUri(request.getRequestURL(), request.getQueryString())
* ...
* .build();
* </pre>
*
* @param requestUrl request URL
* @param queryString query string
* @return This builder
*/
@SuppressWarnings("java:S1149")
public Builder targetUri(StringBuffer requestUrl, String queryString) {
var targetUriTmp = requestUrl.toString();

if (queryString != null) {
targetUriTmp += "?" + queryString;
}

this.targetUri = URI.create(targetUriTmp);

return this;
}

/**
* Adds an HTTP header of given name and value
* <p>
Expand Down Expand Up @@ -220,6 +259,66 @@ public Builder headers(Map<String, ?> headers) {
return this;
}

/**
* Adds HTTP headers.
* <p>
* Method offers compatibility with {@code HttpServletRequest} methods.
* <p>
* Example:
* <pre>
* SignatureContext.builder()
* ...
* .headers(request.getHeaderNames(), request::getHeaders)
* ...
* .build();
* </pre>
*
* @param headerNames enumeration of HTTP servlet request header names
* @param headersGetter getter of HTTP servlet request headers enumeration
* @return This builder
*/
public Builder headers(Enumeration<String> headerNames, Function<String, Enumeration<String>> headersGetter) {
while (headerNames.hasMoreElements()) {
var headerName = headerNames.nextElement();
var headerValues = headersGetter.apply(headerName);

while (headerValues.hasMoreElements()) {
populateHeaderOrTrailer(headers, headerName, headerValues.nextElement());
}
}
return this;
}

/**
* Adds HTTP headers.
* <p>
* Method offers compatibility with {@code HttpServletResponse} methods.
* <p>
* Example:
* <pre>
* SignatureContext.builder()
* ...
* .headers(response.getHeaderNames(), response::getHeaders)
* ...
* .build();
* </pre>
*
* @param headerNames collection of HTTP servlet response header names
* @param headersGetter getter of HTTP servlet response headers collection
* @return This builder
*/
public Builder headers(Collection<String> headerNames, Function<String, Collection<String>> headersGetter) {
for (String headerName : headerNames) {
var headerValues = headersGetter.apply(headerName);

for (String headerValue : headerValues) {
populateHeaderOrTrailer(headers, headerName, headerValue);
}
}

return this;
}

/**
* Adds an HTTP trailer of given name and value
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022-2023 Visma Autopay AS
* Copyright (c) 2022-2024 Visma Autopay AS
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down
Loading

0 comments on commit 8c86de7

Please sign in to comment.