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 13:13:27 UTC

[sling-org-apache-sling-api] branch master updated: 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 master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-api.git


The following commit(s) were added to refs/heads/master by this push:
     new 9c4b93e  SLING-10871 - Add builder API for request/resource objects
9c4b93e is described below

commit 9c4b93e07a34cf08a84b041760bbf55dd96b05f5
Author: Radu Cotescu <17...@users.noreply.github.com>
AuthorDate: Wed Oct 27 15:13:22 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
    * made JavaDocs more consistent
---
 .../apache/sling/api/request/builder/Builders.java | 18 +++--
 .../builder/SlingHttpServletRequestBuilder.java    | 94 +++++++++++-----------
 .../builder/SlingHttpServletResponseBuilder.java   | 17 ++--
 .../builder/SlingHttpServletResponseResult.java    | 52 +++++++-----
 .../builder/impl/SlingHttpServletRequestImpl.java  | 51 +++++++-----
 .../builder/impl/SlingHttpServletResponseImpl.java | 16 ++--
 .../impl/SlingHttpServletRequestImplTest.java      |  6 ++
 .../impl/SlingHttpServletResponseImplTest.java     |  7 ++
 8 files changed, 151 insertions(+), 110 deletions(-)

diff --git a/src/main/java/org/apache/sling/api/request/builder/Builders.java b/src/main/java/org/apache/sling/api/request/builder/Builders.java
index 7e5e5d2..542ad29 100644
--- a/src/main/java/org/apache/sling/api/request/builder/Builders.java
+++ b/src/main/java/org/apache/sling/api/request/builder/Builders.java
@@ -23,12 +23,15 @@ import org.apache.sling.api.request.builder.impl.SlingHttpServletResponseImpl;
 import org.apache.sling.api.resource.Resource;
 import org.jetbrains.annotations.NotNull;
 
-public class Builders {
+public final class Builders {
+
+    private Builders() {}
 
     /**
-     * Create a new request builder
-     * @param resource The resource 
-     * @return A builder
+     * Creates a new request builder.
+     *
+     * @param resource the resource on which the request is based
+     * @return a request builder
      * @throws IllegalArgumentException If resource is {@code null}
      */
     public static @NotNull SlingHttpServletRequestBuilder newRequestBuilder(@NotNull final Resource resource) {
@@ -36,10 +39,11 @@ public class Builders {
     }
 
     /**
-     * Create a new response builder
-     * @return A builder
+     * Creates a new response builder.
+     *
+     * @return a response builder
      */
     public static @NotNull SlingHttpServletResponseBuilder newResponseBuilder() {
         return new SlingHttpServletResponseImpl();
     }
-}
\ No newline at end of file
+}
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..7181985 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
@@ -24,6 +24,7 @@ import javax.servlet.http.HttpServletRequest;
 
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 import org.osgi.annotation.versioning.ProviderType;
 
 /** 
@@ -35,106 +36,109 @@ import org.osgi.annotation.versioning.ProviderType;
 public interface SlingHttpServletRequestBuilder {
 
     /** 
-     * Set the HTTP request method to use - defaults to GET
-     * @param method The HTTP method
+     * Sets the HTTP request method to use - defaults to {@code GET}.
+     * @param method the HTTP method
      * @return this object
      * @throws IllegalArgumentException If method is {@code null}
      */
     @NotNull SlingHttpServletRequestBuilder withRequestMethod(@NotNull String method);
 
     /** 
-     * Set the HTTP request's Content-Type
-     * @param content type
+     * Sets the HTTP request's {@code Content-Type} header.
+     * @param contentType the {@code Content-Type} value
      * @return this object
      */
-    @NotNull SlingHttpServletRequestBuilder withContentType(String contentType);
+    @NotNull SlingHttpServletRequestBuilder withContentType(@Nullable String contentType);
 
     /** 
-     * Use the supplied content as the request's body content
-     * @param content the content
+     * Uses the supplied content as the request's body content.
+     * @param content the request body content
      * @return this object
      */
-    @NotNull SlingHttpServletRequestBuilder withBody(String content);
+    @NotNull SlingHttpServletRequestBuilder withBody(@Nullable String content);
 
     /**
-     * Sets the optional selectors of the internal request, which influence
-     * the Servlet/Script resolution.
-     * @param selectors The selectors
+     * Sets the optional selectors of the internal request, which influence the Servlet/Script resolution.
+     * @param selectors the selectors
      * @return this object
      */
-    @NotNull SlingHttpServletRequestBuilder withSelectors(String ... selectors);
+    @NotNull SlingHttpServletRequestBuilder withSelectors(@Nullable String ... selectors);
 
     /** 
-     * Sets the optional extension of the internal request, which influence
-     * the Servlet/Script resolution.
-     * @param extension The extension
+     * 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);
+    @NotNull SlingHttpServletRequestBuilder withExtension(@Nullable String extension);
+
+    /**
+     * Sets the optional suffix of the internal request.
+     * @param suffix the suffix
+     * @return this object
+     */
+    @NotNull SlingHttpServletRequestBuilder withSuffix(@Nullable String suffix);
 
     /** 
-     * Set a request parameter
-     * @param key The name of the parameter
-     * @param value The value of the parameter
+     * Sets a request parameter
+     * @param key the name of the parameter
+     * @param value the value of the parameter
      * @return this object
-     * @throws IllegalArgumentException If key or value is {@code null}
+     * @throws IllegalArgumentException if either {@code key} or {@code value} is {@code null}
      */
     @NotNull SlingHttpServletRequestBuilder withParameter(@NotNull String key, @NotNull String value);
 
     /** 
-     * Set a request parameter
-     * @param key The name of the parameter
-     * @param values The values of the parameter
+     * Sets a request parameter
+     * @param key the name of the parameter
+     * @param values the values of the parameter
      * @return this object
-     * @throws IllegalArgumentException If key or values is {@code null}
+     * @throws IllegalArgumentException if either {@code key} or {@code values} is {@code null}
      */
     @NotNull SlingHttpServletRequestBuilder withParameter(@NotNull String key, @NotNull String[] values);
 
     /** 
-     * Add the supplied request parameters to the current ones.
-     * @param parameters Additional parameters
+     * Adds 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(@Nullable Map<String, String[]> parameters);
 
     /** 
-     * Use the request dispatcher from the provided request.
-     * @param request The request
+     * Uses the request dispatcher from the provided request.
+     * @param request the request from which to use the dispatcher
      * @return this object
-     * @throws IllegalArgumentException If request is {@code null}
+     * @throws IllegalArgumentException if {@code request} is {@code null}
      */
     @NotNull SlingHttpServletRequestBuilder useRequestDispatcherFrom(@NotNull SlingHttpServletRequest request);
 
     /** 
-     * If a session is used, use the session from the provided request
-     * @param request The request
+     * Uses the session from the provided request.
+     * @param request the request from which to use the session
      * @return this object
-     * @throws IllegalArgumentException If request is {@code null}
+     * @throws IllegalArgumentException if {@code request} is {@code null}
      */
     @NotNull SlingHttpServletRequestBuilder useSessionFrom(@NotNull HttpServletRequest request);
 
     /** 
-     * Use the attributes backed by the provided request
-     * @param request The request
+     * Uses the attributes backed by the provided request.
+     * @param request the request from which to use the attributes
      * @return this object
-     * @throws IllegalArgumentException If request is {@code null}
+     * @throws IllegalArgumentException if {@code request} is {@code null}
      */
     @NotNull SlingHttpServletRequestBuilder useAttributesFrom(@NotNull HttpServletRequest request);
 
     /** 
-     * Use the servlet context from the provided request
-     * @param request The request
+     * Uses the servlet context from the provided request
+     * @param request the request from which to use the servlet context
      * @return this object
-     * @throws IllegalArgumentException If request is {@code null}
+     * @throws IllegalArgumentException if {@code request} is {@code null}
      */
     @NotNull SlingHttpServletRequestBuilder useServletContextFrom(@NotNull HttpServletRequest request);
 
     /**
-     * Build the request.
-     * Once this method has been called, the builder must not be used anymore. A new builder
-     * needs to be created, to create a new request.
-     * @return A request object
+     * Builds the request. Once this method has been called, the builder must not be used anymore. In order to create a new request a new
+     * builder has to be used.
+     * @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/SlingHttpServletResponseBuilder.java b/src/main/java/org/apache/sling/api/request/builder/SlingHttpServletResponseBuilder.java
index 23f4752..71b7fb6 100644
--- a/src/main/java/org/apache/sling/api/request/builder/SlingHttpServletResponseBuilder.java
+++ b/src/main/java/org/apache/sling/api/request/builder/SlingHttpServletResponseBuilder.java
@@ -21,19 +21,18 @@ package org.apache.sling.api.request.builder;
 import org.jetbrains.annotations.NotNull;
 import org.osgi.annotation.versioning.ProviderType;
 
-/** 
- * Fluent helper for building a response.
- * 
- * Instances of this interface are not thread-safe.
+/**
+ * <p>Fluent helper for building a response.</p>
+ * <p><strong>Note:</strong> instances of this interface are not thread-safe.</p>
  */
 @ProviderType
 public interface SlingHttpServletResponseBuilder {
 
     /**
-     * Build the response.
-     * Once this method has been called, the builder must not be used anymore. A new builder
-     * needs to be created, to create a new response.
-     * @return A response object
+     * Builds the response. Once this method has been called, the builder must not be used anymore. In order to create a new response a new
+     * builder has to be used.
+     *
+     * @return a response object
      */
     @NotNull SlingHttpServletResponseResult 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..081d2e5 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,49 +22,57 @@ 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;
 
-/** 
- * This is an extension of a {@link SlingHttpServletResponse} to get
- * the result from a processing.
- * 
- * Instances of this interface are not thread-safe.
+/**
+ * <p>This is an extension of a {@link SlingHttpServletResponse} to get the result from a processing operation.</p>
+ *
+ * <p><strong>Note:</strong> instances of this interface are not thread-safe.</p>
  */
 @ProviderType
 public interface SlingHttpServletResponseResult extends SlingHttpServletResponse {
 
     /**
-     * Get the content length
-     * @return The content length or {@code -1} if not set
+     * Gets the content length
+     *
+     * @return the content length or {@code -1} if not set
      */
     long getContentLength();
 
     /**
-     * Get the status message
-     * @return The status message or {@code null}.
+     * Gets 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.
+     * Gets 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.
+     * Gets all cookies.
+     *
+     * @return the array of cookies or {@code null} if no cookies were set
      */
-    Cookie[] getCookies();
+    @Nullable Cookie[] getCookies();
 
     /**
-     * Get the output as a byte array
+     * Gets the output as a byte array.
+     *
+     * @return the output as a byte array
      */
-    byte [] getOutput();
+    byte[] getOutput();
 
     /**
-     * Get the output as a string
+     * Gets the output as a string.
+     *
+     * @return 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..66d1562 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
@@ -31,9 +31,8 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.Dictionary;
 import java.util.Enumeration;
-import java.util.Hashtable;
+import java.util.HashMap;
 import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.ListResourceBundle;
@@ -68,6 +67,7 @@ import org.apache.sling.api.request.builder.SlingHttpServletRequestBuilder;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 
 /**
  * Internal {@link SlingHttpServletRequest} implementation.
@@ -90,6 +90,7 @@ public class SlingHttpServletRequestImpl extends SlingAdaptable
             return new Object[0][0];
         }
     };
+    private static final String REQUEST = "request";
 
     /** Required resource */
     private final Resource resource;
@@ -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;
@@ -198,21 +202,20 @@ public class SlingHttpServletRequestImpl extends SlingAdaptable
     }
 
     @Override
-    public @NotNull SlingHttpServletRequestBuilder withContentType(final String type) {
+    public @NotNull SlingHttpServletRequestBuilder withContentType(final @Nullable 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());
+        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;
         }
-
         return this;
     }
 
     @Override
-    public @NotNull SlingHttpServletRequestBuilder withBody(final String content) {
+    public @NotNull SlingHttpServletRequestBuilder withBody(final @Nullable String content) {
         this.checkLocked();
         this.body = content;
         return this;
@@ -233,6 +236,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,17 +261,18 @@ public class SlingHttpServletRequestImpl extends SlingAdaptable
     }
 
     @Override
-    public @NotNull SlingHttpServletRequestBuilder withParameters(final @NotNull  Map<String, String[]> parameters) {
+    public @NotNull SlingHttpServletRequestBuilder withParameters(final @Nullable Map<String, String[]> parameters) {
         this.checkLocked();
-        this.checkNotNull("parameters", parameters);
-        this.parameters.putAll(parameters);
+        if (parameters != null) {
+            this.parameters.putAll(parameters);
+        }
         return this;
     }
 
     @Override
     public @NotNull SlingHttpServletRequestBuilder useAttributesFrom(@NotNull HttpServletRequest request) {
         this.checkLocked();
-        this.checkNotNull("request", request);
+        this.checkNotNull(REQUEST, request);
         this.attributesProvider = request;
         return this;
     }
@@ -269,7 +280,7 @@ public class SlingHttpServletRequestImpl extends SlingAdaptable
     @Override
     public @NotNull SlingHttpServletRequestBuilder useServletContextFrom(@NotNull HttpServletRequest request) {
         this.checkLocked();
-        this.checkNotNull("request", request);
+        this.checkNotNull(REQUEST, request);
         this.servletContext = request.getServletContext();
         return this;
     }
@@ -277,7 +288,7 @@ public class SlingHttpServletRequestImpl extends SlingAdaptable
     @Override
     public @NotNull SlingHttpServletRequestBuilder useSessionFrom(@NotNull HttpServletRequest request) {
         this.checkLocked();
-        this.checkNotNull("request", request);
+        this.checkNotNull(REQUEST, request);
         this.sessionProvider = request;
         return this;
     }
@@ -285,7 +296,7 @@ public class SlingHttpServletRequestImpl extends SlingAdaptable
     @Override
     public @NotNull SlingHttpServletRequestBuilder useRequestDispatcherFrom(@NotNull SlingHttpServletRequest request) {
         this.checkLocked();
-        this.checkNotNull("request", request);
+        this.checkNotNull(REQUEST, request);
         this.requestDispatcherProvider = request;
         return this;
     }
@@ -295,7 +306,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 +417,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
@@ -471,7 +482,7 @@ public class SlingHttpServletRequestImpl extends SlingAdaptable
 
     @Override
     public List<RequestParameter> getRequestParameterList() {
-        final List<RequestParameter> params = new ArrayList<RequestParameter>();
+        final List<RequestParameter> params = new ArrayList<>();
         for (final RequestParameter[] requestParameters : getRequestParameterMap().values()) {
             params.addAll(Arrays.asList(requestParameters));
         }
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..5cf1e41 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"));
@@ -267,6 +268,11 @@ public class SlingHttpServletRequestImplTest {
         assertEquals("text/text;charset=UTF-8", req.getContentType());
     }
 
+    @Test public void testNullContentType() {
+        req.withContentType(null).build();
+        assertNull("null", req.getContentType());
+    }
+
     @Test public void testContentTypeAndCharset() throws UnsupportedEncodingException {
         req.withContentType("text/text;charset=UTF-16").build();
         assertEquals("text/text;charset=UTF-16", req.getContentType());
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 {