You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ra...@apache.org on 2021/10/27 08:57:51 UTC
[sling-org-apache-sling-api] 01/01: SLING-10871 - Add builder API
for request/resource objects
This is an automated email from the ASF dual-hosted git repository.
radu pushed a commit to branch issue/SLING-10871
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-api.git
commit 20a4fc0d12d95a6c3a29bd3ad108555578ce0c38
Author: Radu Cotescu <ra...@apache.org>
AuthorDate: Wed Oct 27 10:56:15 2021 +0200
SLING-10871 - Add builder API for request/resource objects
* allow passing nulls to methods accepting only one parameter so
that clients can still use fluid calls without doing null checks
* adjusted tests to reflect the new behaviour
---
.../builder/SlingHttpServletRequestBuilder.java | 14 +++++---
.../builder/SlingHttpServletResponseResult.java | 11 +++---
.../builder/impl/SlingHttpServletRequestImpl.java | 39 ++++++++++++++--------
.../builder/impl/SlingHttpServletResponseImpl.java | 16 +++++----
.../impl/SlingHttpServletRequestImplTest.java | 3 ++
.../impl/SlingHttpServletResponseImplTest.java | 7 ++++
6 files changed, 61 insertions(+), 29 deletions(-)
diff --git a/src/main/java/org/apache/sling/api/request/builder/SlingHttpServletRequestBuilder.java b/src/main/java/org/apache/sling/api/request/builder/SlingHttpServletRequestBuilder.java
index 9f661a8..1eec3a5 100644
--- a/src/main/java/org/apache/sling/api/request/builder/SlingHttpServletRequestBuilder.java
+++ b/src/main/java/org/apache/sling/api/request/builder/SlingHttpServletRequestBuilder.java
@@ -65,13 +65,20 @@ public interface SlingHttpServletRequestBuilder {
@NotNull SlingHttpServletRequestBuilder withSelectors(String ... selectors);
/**
- * Sets the optional extension of the internal request, which influence
+ * Sets the optional extension of the internal request, which influences
* the Servlet/Script resolution.
* @param extension The extension
* @return this object
*/
@NotNull SlingHttpServletRequestBuilder withExtension(String extension);
+ /**
+ * Sets the optional suffix of the internal request.
+ * @param suffix The suffix
+ * @return this object
+ */
+ @NotNull SlingHttpServletRequestBuilder withSuffix(String suffix);
+
/**
* Set a request parameter
* @param key The name of the parameter
@@ -94,9 +101,8 @@ public interface SlingHttpServletRequestBuilder {
* Add the supplied request parameters to the current ones.
* @param parameters Additional parameters
* @return this object
- * @throws IllegalArgumentException If parameters is {@code null}
*/
- @NotNull SlingHttpServletRequestBuilder withParameters(@NotNull Map<String, String[]> parameters);
+ @NotNull SlingHttpServletRequestBuilder withParameters(Map<String, String[]> parameters);
/**
* Use the request dispatcher from the provided request.
@@ -137,4 +143,4 @@ public interface SlingHttpServletRequestBuilder {
* @return A request object
*/
@NotNull SlingHttpServletRequest build();
-}
\ No newline at end of file
+}
diff --git a/src/main/java/org/apache/sling/api/request/builder/SlingHttpServletResponseResult.java b/src/main/java/org/apache/sling/api/request/builder/SlingHttpServletResponseResult.java
index 7dc1e9c..2bbc903 100644
--- a/src/main/java/org/apache/sling/api/request/builder/SlingHttpServletResponseResult.java
+++ b/src/main/java/org/apache/sling/api/request/builder/SlingHttpServletResponseResult.java
@@ -22,6 +22,7 @@ import javax.servlet.http.Cookie;
import org.apache.sling.api.SlingHttpServletResponse;
import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
import org.osgi.annotation.versioning.ProviderType;
/**
@@ -43,20 +44,20 @@ public interface SlingHttpServletResponseResult extends SlingHttpServletResponse
* Get the status message
* @return The status message or {@code null}.
*/
- String getStatusMessage();
+ @Nullable String getStatusMessage();
/**
* Get the named cookie
* @param name The name of the cookie
* @return The cookie or {@code null} if no cookie with that name exists.
*/
- Cookie getCookie(String name);
+ @Nullable Cookie getCookie(String name);
/**
* Get all cookies
* @return The array of cookies or {@code null} if no cookies are set.
*/
- Cookie[] getCookies();
+ @Nullable Cookie[] getCookies();
/**
* Get the output as a byte array
@@ -66,5 +67,5 @@ public interface SlingHttpServletResponseResult extends SlingHttpServletResponse
/**
* Get the output as a string
*/
- String getOutputAsString();
-}
\ No newline at end of file
+ @NotNull String getOutputAsString();
+}
diff --git a/src/main/java/org/apache/sling/api/request/builder/impl/SlingHttpServletRequestImpl.java b/src/main/java/org/apache/sling/api/request/builder/impl/SlingHttpServletRequestImpl.java
index 9ca89dd..6f81be1 100644
--- a/src/main/java/org/apache/sling/api/request/builder/impl/SlingHttpServletRequestImpl.java
+++ b/src/main/java/org/apache/sling/api/request/builder/impl/SlingHttpServletRequestImpl.java
@@ -33,6 +33,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
+import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.List;
@@ -100,6 +101,9 @@ public class SlingHttpServletRequestImpl extends SlingAdaptable
/** Optional extension */
private String extension;
+ /** Optional suffix */
+ private String suffix;
+
/** HTTP method */
private String requestMethod = DEFAULT_METHOD;
@@ -126,7 +130,7 @@ public class SlingHttpServletRequestImpl extends SlingAdaptable
private String pathInfo;
/** Attributes */
- private final Dictionary<String, Object> attributeMap = new Hashtable<>();
+ private final Map<String, Object> attributeMap = new HashMap<>();
/** On demand session */
private HttpSession session;
@@ -200,14 +204,15 @@ public class SlingHttpServletRequestImpl extends SlingAdaptable
@Override
public @NotNull SlingHttpServletRequestBuilder withContentType(final String type) {
this.checkLocked();
- final int pos = type.indexOf(SlingHttpServletRequestImpl.CHARSET_SEPARATOR);
- if ( pos != -1 ) {
- this.contentType = type.substring(0, pos);
- this.characterEncoding = type.substring(pos + SlingHttpServletRequestImpl.CHARSET_SEPARATOR.length());
- } else {
- this.contentType = type;
+ if (type != null) {
+ final int pos = type.indexOf(SlingHttpServletRequestImpl.CHARSET_SEPARATOR);
+ if (pos != -1) {
+ this.contentType = type.substring(0, pos);
+ this.characterEncoding = type.substring(pos + SlingHttpServletRequestImpl.CHARSET_SEPARATOR.length());
+ } else {
+ this.contentType = type;
+ }
}
-
return this;
}
@@ -233,6 +238,13 @@ public class SlingHttpServletRequestImpl extends SlingAdaptable
}
@Override
+ public @NotNull SlingHttpServletRequestBuilder withSuffix(String suffix) {
+ this.checkLocked();
+ this.suffix = suffix;
+ return this;
+ }
+
+ @Override
public @NotNull SlingHttpServletRequestBuilder withParameter(final @NotNull String key, final @NotNull String value) {
this.checkLocked();
this.checkNotNull("key", key);
@@ -251,10 +263,11 @@ public class SlingHttpServletRequestImpl extends SlingAdaptable
}
@Override
- public @NotNull SlingHttpServletRequestBuilder withParameters(final @NotNull Map<String, String[]> parameters) {
+ public @NotNull SlingHttpServletRequestBuilder withParameters(final Map<String, String[]> parameters) {
this.checkLocked();
- this.checkNotNull("parameters", parameters);
- this.parameters.putAll(parameters);
+ if (parameters != null) {
+ this.parameters.putAll(parameters);
+ }
return this;
}
@@ -295,7 +308,7 @@ public class SlingHttpServletRequestImpl extends SlingAdaptable
this.checkLocked();
this.locked = true;
- this.requestPathInfo = new RequestPathInfoImpl(this.resource, this.selectors, this.extension, null);
+ this.requestPathInfo = new RequestPathInfoImpl(this.resource, this.selectors, this.extension, this.suffix);
this.queryString = this.formatQueryString();
this.pathInfo = this.buildPathInfo();
@@ -406,7 +419,7 @@ public class SlingHttpServletRequestImpl extends SlingAdaptable
if ( this.attributesProvider != null ) {
return this.attributesProvider.getAttributeNames();
}
- return this.attributeMap.keys();
+ return Collections.enumeration(this.attributeMap.keySet());
}
@Override
diff --git a/src/main/java/org/apache/sling/api/request/builder/impl/SlingHttpServletResponseImpl.java b/src/main/java/org/apache/sling/api/request/builder/impl/SlingHttpServletResponseImpl.java
index 5ebeec6..f9df5a4 100644
--- a/src/main/java/org/apache/sling/api/request/builder/impl/SlingHttpServletResponseImpl.java
+++ b/src/main/java/org/apache/sling/api/request/builder/impl/SlingHttpServletResponseImpl.java
@@ -120,12 +120,14 @@ public class SlingHttpServletResponseImpl
@Override
public void setContentType(final String type) {
- final int pos = type.indexOf(SlingHttpServletRequestImpl.CHARSET_SEPARATOR);
- if ( pos != -1 ) {
- this.contentType = type.substring(0, pos);
- this.characterEncoding = type.substring(pos + SlingHttpServletRequestImpl.CHARSET_SEPARATOR.length());
- } else {
- this.contentType = type;
+ if (this.printWriter == null) {
+ final int pos = type == null ? -1 : type.indexOf(SlingHttpServletRequestImpl.CHARSET_SEPARATOR);
+ if (pos != -1) {
+ this.contentType = type.substring(0, pos);
+ this.characterEncoding = type.substring(pos + SlingHttpServletRequestImpl.CHARSET_SEPARATOR.length());
+ } else {
+ this.contentType = type;
+ }
}
}
@@ -359,7 +361,7 @@ public class SlingHttpServletResponseImpl
}
@Override
- public String getOutputAsString() {
+ public @NotNull String getOutputAsString() {
this.isCommitted = true;
return new String(getOutput(), this.getCharset());
}
diff --git a/src/test/java/org/apache/sling/api/request/builder/impl/SlingHttpServletRequestImplTest.java b/src/test/java/org/apache/sling/api/request/builder/impl/SlingHttpServletRequestImplTest.java
index 49c3a37..c490daa 100644
--- a/src/test/java/org/apache/sling/api/request/builder/impl/SlingHttpServletRequestImplTest.java
+++ b/src/test/java/org/apache/sling/api/request/builder/impl/SlingHttpServletRequestImplTest.java
@@ -140,6 +140,7 @@ public class SlingHttpServletRequestImplTest {
req.withParameter("a", "b");
req.withParameter("c", new String[] {"d", "e"});
req.withParameters(Collections.singletonMap("f", new String[] {"g"}));
+ req.withParameters(null);
req.build();
assertEquals("b", req.getParameter("a"));
@@ -265,6 +266,8 @@ public class SlingHttpServletRequestImplTest {
req.setCharacterEncoding("UTF-8");
assertEquals("UTF-8", req.getCharacterEncoding());
assertEquals("text/text;charset=UTF-8", req.getContentType());
+ req.withContentType(null);
+ assertNull("null", req.getContentType());
}
@Test public void testContentTypeAndCharset() throws UnsupportedEncodingException {
diff --git a/src/test/java/org/apache/sling/api/request/builder/impl/SlingHttpServletResponseImplTest.java b/src/test/java/org/apache/sling/api/request/builder/impl/SlingHttpServletResponseImplTest.java
index 8543788..c7b1bbd 100644
--- a/src/test/java/org/apache/sling/api/request/builder/impl/SlingHttpServletResponseImplTest.java
+++ b/src/test/java/org/apache/sling/api/request/builder/impl/SlingHttpServletResponseImplTest.java
@@ -76,6 +76,13 @@ public class SlingHttpServletResponseImplTest {
res.setCharacterEncoding("UTF-8");
assertEquals("UTF-8", res.getCharacterEncoding());
assertEquals("text/text;charset=UTF-8", res.getContentType());
+
+ res.setContentType(null);
+ assertNull("null", res.getContentType());
+ res.getWriter();
+ // this should not be possible anymore, since a writer was created
+ res.setContentType("text/text");
+ assertNull("null", res.getContentType());
}
@Test public void testContentTypeAndCharset() throws UnsupportedEncodingException {