Anonymous avatar Anonymous committed bbc4bf7

Encapsulate restlet headers access

Comments (0)

Files changed (5)

chempound-webapp/src/main/java/net/chempound/webapp/Restlet.java

+package net.chempound.webapp;
+
+import org.restlet.Request;
+import org.restlet.Response;
+import org.restlet.data.Form;
+import org.restlet.engine.http.header.HeaderConstants;
+
+import java.util.Map;
+
+/**
+ * @author Sam Adams
+ */
+public class Restlet {
+
+    private static final String ATTRIBUTE_HEADERS = HeaderConstants.ATTRIBUTE_HEADERS;
+
+    public static Form getHeaders(final Request request) {
+        return (Form) request.getAttributes().get(ATTRIBUTE_HEADERS);
+    }
+
+    public static void setHeader(final Response response, final String name, final String value) {
+        getOrCreateHeaders(response).set(name, value);
+    }
+
+    private static Form getOrCreateHeaders(final Response response) {
+        final Map<String,Object> attributes = response.getAttributes();
+        Form headers = (Form) attributes.get(ATTRIBUTE_HEADERS);
+        if (headers == null) {
+            headers = new Form();
+            attributes.put(ATTRIBUTE_HEADERS, headers);
+        }
+        return headers;
+    }
+}

chempound-webapp/src/main/java/net/chempound/webapp/RestletConstants.java

-package net.chempound.webapp;
-
-import org.restlet.engine.http.header.HeaderConstants;
-
-/**
- * @author Sam Adams
- */
-public class RestletConstants {
-
-    public static final String ATTRIBUTE_HEADERS = HeaderConstants.ATTRIBUTE_HEADERS;
-
-}

chempound-webapp/src/main/java/net/chempound/webapp/content/CollectionResource.java

 package net.chempound.webapp.content;
 
 import net.chempound.datastore.TripleStore;
+import net.chempound.webapp.Restlet;
 import net.chempound.webapp.splashpage.SplashPageGenerator;
 import net.chempound.webapp.sword.AbderaRepresentation;
 import org.apache.abdera.Abdera;
 import java.io.IOException;
 import java.io.InputStream;
 
-import static net.chempound.webapp.RestletConstants.ATTRIBUTE_HEADERS;
 import static org.restlet.data.MediaType.APPLICATION_ATOM;
 
 /**
     }
 
     private String getSlug() {
-        final Form headers = (Form) getRequestAttributes().get(ATTRIBUTE_HEADERS);
+        final Form headers = Restlet.getHeaders(getRequest());
         return headers.getFirstValue("Slug", IGNORE_CASE);
     }
 
     protected boolean isInProgress() throws SwordError {
-        final Form headers = (Form) getRequestAttributes().get(ATTRIBUTE_HEADERS);
+        final Form headers = Restlet.getHeaders(getRequest());
         final String iph = headers.getFirstValue("In-Progress", IGNORE_CASE);
 
         boolean inProgress = false; // default value
     }
 
     private String getPackaging() {
-        final Form headers = (Form) getRequestAttributes().get(ATTRIBUTE_HEADERS);
+        final Form headers = Restlet.getHeaders(getRequest());
         String packaging = headers.getFirstValue("Packaging", IGNORE_CASE);
         if (packaging == null || "".equals(packaging)) {
             packaging = UriRegistry.PACKAGE_BINARY;
         if (getRequestEntity().getDisposition() != null) {
             filename = getRequestEntity().getDisposition().getFilename();
         } else {
-            final Form headers = (Form) getRequestAttributes().get(ATTRIBUTE_HEADERS);
+            final Form headers = Restlet.getHeaders(getRequest());
             final String cd = headers.getFirstValue("Content-Disposition", IGNORE_CASE);
             if (cd != null) {
                 final int i = cd.indexOf("filename=");

chempound-webapp/src/main/java/net/chempound/webapp/content/PingbackProcessor.java

 package net.chempound.webapp.content;
 
 import net.chempound.config.BaseUri;
+import net.chempound.webapp.Restlet;
 import org.restlet.Response;
-import org.restlet.data.Form;
 
 import javax.inject.Inject;
 import javax.inject.Singleton;
 import java.net.URI;
-import java.util.Map;
-
-import static net.chempound.webapp.RestletConstants.ATTRIBUTE_HEADERS;
 
 @Singleton
 public class PingbackProcessor {
     }
 
     public void addPingbackHeader(final Response response) {
-        getHeaders(response).set(HEADER_PINGBACK, pingbackUrl.toString());
+        Restlet.setHeader(response, HEADER_PINGBACK, pingbackUrl.toString());
     }
-
-    private Form getHeaders(final Response response) {
-        final Map<String,Object> attributes = response.getAttributes();
-        Form headers = (Form) attributes.get(ATTRIBUTE_HEADERS);
-        if (headers == null) {
-            headers = new Form();
-            attributes.put(ATTRIBUTE_HEADERS, headers);
-        }
-        return headers;
-    }
-
 }

chempound-webapp/src/main/java/net/chempound/webapp/sword/CollectionResource.java

 package net.chempound.webapp.sword;
 
+import net.chempound.webapp.Restlet;
 import org.apache.abdera.Abdera;
 import org.apache.abdera.i18n.iri.IRI;
 import org.apache.abdera.model.Document;
 import java.io.IOException;
 import java.io.InputStream;
 
-import static net.chempound.webapp.RestletConstants.ATTRIBUTE_HEADERS;
 import static org.restlet.data.MediaType.APPLICATION_ATOM;
 
 /**
     }
 
     private String getSlug() {
-        final Form headers = (Form) getRequestAttributes().get(ATTRIBUTE_HEADERS);
+        final Form headers = Restlet.getHeaders(getRequest());
         return headers.getFirstValue("Slug", IGNORE_CASE);
     }
 
     protected boolean isInProgress() throws SwordError {
-        final Form headers = (Form) getRequestAttributes().get(ATTRIBUTE_HEADERS);
+        final Form headers = Restlet.getHeaders(getRequest());
         final String iph = headers.getFirstValue("In-Progress", IGNORE_CASE);
 
         boolean inProgress = false; // default value
     }
 
     private String getPackaging() {
-        final Form headers = (Form) getRequestAttributes().get(ATTRIBUTE_HEADERS);
+        final Form headers = Restlet.getHeaders(getRequest());
         String packaging = headers.getFirstValue("Packaging", IGNORE_CASE);
         if (packaging == null || "".equals(packaging)) {
             packaging = UriRegistry.PACKAGE_BINARY;
         if (getRequestEntity().getDisposition() != null) {
             filename = getRequestEntity().getDisposition().getFilename();
         } else {
-            final Form headers = (Form) getRequestAttributes().get(ATTRIBUTE_HEADERS);
+            final Form headers = Restlet.getHeaders(getRequest());
             final String cd = headers.getFirstValue("Content-Disposition", IGNORE_CASE);
             if (cd != null) {
                 final int i = cd.indexOf("filename=");
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.