Commits

Anonymous committed a59fcd4

Extract JsonGenerator from ResourceMapResource

  • Participants
  • Parent commits f26a751

Comments (0)

Files changed (5)

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

+package net.chempound.webapp.content;
+
+import com.hp.hpl.jena.rdf.model.Model;
+import net.chempound.datastore.TripleStore;
+import net.chempound.rdf.chempound.ChempoundAggregation;
+import net.chempound.rdf.ore.AggregatedResource;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import java.net.URI;
+
+@Singleton
+public class JsonGenerator {
+
+    private static final String URI = "uri";
+    private static final String TITLE = "title";
+    private static final String RESOURCES = "resources";
+
+    private final TripleStore tripleStore;
+
+    @Inject
+    public JsonGenerator(final TripleStore tripleStore) {
+        this.tripleStore = tripleStore;
+    }
+
+    public JSONObject createJson(final URI aggregationUri) throws JSONException {
+        final Model model = tripleStore.getModel(aggregationUri);
+        final ChempoundAggregation aggregation = model.getResource(aggregationUri.toString()).as(ChempoundAggregation.class);
+
+        final JSONObject obj = new JSONObject();
+        obj.put(URI, aggregation.getURI());
+        obj.put(TITLE, aggregation.getTitle());
+        obj.put(RESOURCES, createResourceLinks(aggregation));
+        return obj;
+    }
+
+    private JSONArray createResourceLinks(final ChempoundAggregation aggregation) throws JSONException {
+        final JSONArray resources = new JSONArray();
+        for (final AggregatedResource resource : aggregation.getAggregatedResources()) {
+            resources.put(createResourceLink(resource));
+        }
+        return resources;
+    }
+
+    private JSONObject createResourceLink(final AggregatedResource resource) throws JSONException {
+        final JSONObject json = new JSONObject();
+        json.put(URI, resource.getURI());
+        return json;
+    }
+}

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

 import com.hp.hpl.jena.rdf.model.Model;
 import net.chempound.config.BaseUri;
 import net.chempound.datastore.TripleStore;
-import net.chempound.rdf.chempound.ChempoundAggregation;
-import net.chempound.rdf.ore.AggregatedResource;
 import net.chempound.webapp.splashpage.SplashPageGenerator;
 import net.chempound.webapp.utils.ByteArrayRepresentation;
-import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.restlet.data.Form;
     private final Provider<SplashPageGenerator> splashPageGeneratorProvider;
     private final URI pingbackUrl;
 
+    private final JsonGenerator jsonGenerator;
+
     @Inject
-    public ResourceMapResource(final TripleStore tripleStore, final Provider<SplashPageGenerator> splashPageGeneratorProvider, @BaseUri final URI baseUri) {
+    public ResourceMapResource(final TripleStore tripleStore, final Provider<SplashPageGenerator> splashPageGeneratorProvider, @BaseUri final URI baseUri, final JsonGenerator jsonGenerator) {
         super(tripleStore);
         this.splashPageGeneratorProvider = splashPageGeneratorProvider;
+        this.jsonGenerator = jsonGenerator;
         this.pingbackUrl = baseUri.resolve("pingback");
     }
 
 
     @Get("json")
     public Representation getJson() {
-        final JSONObject obj = createJson();
+        try {
+            final JSONObject obj = jsonGenerator.createJson(aggregationUri);
+            return toRepresentation(obj);
+       } catch (JSONException e) {
+            throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Error building JSON", e);
+        }
+    }
+
+    private Representation toRepresentation(final JSONObject obj) {
         final JsonRepresentation representation = new JsonRepresentation(obj);
         representation.setIndenting(true);
         representation.setIndentingSize(2);
     }
 
     private void addPingbackHeader() {
+        getHeaders().set("X-Pingback", pingbackUrl.toString());
+    }
+
+    private Form getHeaders() {
         Form headers = (Form) getResponseAttributes().get(ATTRIBUTE_HEADERS);
         if (headers == null) {
             headers = new Form();
             getResponseAttributes().put(ATTRIBUTE_HEADERS, headers);
         }
-        headers.set("X-Pingback", pingbackUrl.toString());
+        return headers;
     }
 
     private Representation getRdf(final MediaType mediaType) {
         model.write(buffer, lang);
         return buffer.toByteArray();
     }
-
-    private JSONObject createJson() {
-        final JSONObject obj = new JSONObject();
-
-        try {
-            final Model model = tripleStore.getModel(aggregationUri);
-            final String uri = aggregationUri.toString();
-            final ChempoundAggregation aggregation = model.getResource(uri).as(ChempoundAggregation.class);
-            obj.put("uri", uri);
-            obj.put("title", aggregation.getTitle());
-
-            final JSONArray resources = new JSONArray();
-            for (final AggregatedResource r : aggregation.getAggregatedResources()) {
-                final JSONObject resource = new JSONObject();
-                resource.put("uri", r.getURI());
-                resources.put(resource);
-            }
-            obj.put("resources", resources);
-        } catch (JSONException e) {
-            throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Error building JSON", e);
-        }
-
-        return obj;
-    }
-
 }

chempound-webapp/src/main/java/net/chempound/webapp/sword/impl/DepositReceiptFactory.java

 import org.apache.abdera.i18n.iri.IRI;
 import org.swordapp.server.DepositReceipt;
 
+import javax.inject.Singleton;
 import java.net.URI;
 
+@Singleton
 public class DepositReceiptFactory {
 
     public DepositReceipt createDepositReceipt(final boolean packaged, final URI swordBase, final DepositResponse response, final URI uri, final URI relative) {

chempound-webapp/src/main/java/net/chempound/webapp/sword/impl/DepositRequestFactory.java

 import org.swordapp.server.*;
 
 import javax.inject.Inject;
+import javax.inject.Singleton;
 import javax.xml.namespace.QName;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 
+@Singleton
 public class DepositRequestFactory {
 
     private static final Logger LOG = LoggerFactory.getLogger(DepositRequestFactory.class);

chempound-webapp/src/test/java/net/chempound/webapp/content/ResourceMapResourceTest.java

 
     private TripleStore tripleStore;
     private Provider<SplashPageGenerator> splashProvider;
+    private JsonGenerator jsonGenerator;
 
     @Before
     public void setup() throws Exception {
         addResourceMap(aggregation, "http://repo.example.net/content/foo/foo.n3", MimeType.TEXT_RDF_N3);
         addAggregatedResource(aggregation, "http://repo.example.net/content/foo/bar.txt");
         tripleStore.saveModel(URI.create("http://repo.example.net/content/foo/"), model);
+        jsonGenerator = new JsonGenerator(tripleStore);
     }
 
     @After
         Request request = new Request(Method.GET, "http://repo.example.net/content/foo/foo.html");
         Response response = new Response(request);
 
-        ResourceMapResource resource = new ResourceMapResource(tripleStore, splashProvider, BASE_URI);
+        ResourceMapResource resource = new ResourceMapResource(tripleStore, splashProvider, BASE_URI, jsonGenerator);
         resource.init(null, request, response);
 
         resource.handle();
         Request request = new Request(Method.GET, "http://repo.example.net/content/foo/foo.rdf");
         Response response = new Response(request);
 
-        ResourceMapResource resource = new ResourceMapResource(tripleStore, splashProvider, BASE_URI);
+        ResourceMapResource resource = new ResourceMapResource(tripleStore, splashProvider, BASE_URI, jsonGenerator);
         resource.init(null, request, response);
 
         resource.handle();
         Request request = new Request(Method.GET, "http://repo.example.net/content/foo/foo.ttl");
         Response response = new Response(request);
 
-        ResourceMapResource resource = new ResourceMapResource(tripleStore, splashProvider, BASE_URI);
+        ResourceMapResource resource = new ResourceMapResource(tripleStore, splashProvider, BASE_URI, jsonGenerator);
         resource.init(null, request, response);
 
         resource.handle();
         Request request = new Request(Method.GET, "http://repo.example.net/content/foo/foo.n3");
         Response response = new Response(request);
 
-        ResourceMapResource resource = new ResourceMapResource(tripleStore, splashProvider, BASE_URI);
+        ResourceMapResource resource = new ResourceMapResource(tripleStore, splashProvider, BASE_URI, jsonGenerator);
         resource.init(null, request, response);
 
         resource.handle();
         Request request = new Request(Method.GET, "http://repo.example.net/content/foo/foo.json");
         Response response = new Response(request);
 
-        ResourceMapResource resource = new ResourceMapResource(tripleStore, splashProvider, BASE_URI);
+        ResourceMapResource resource = new ResourceMapResource(tripleStore, splashProvider, BASE_URI, jsonGenerator);
         resource.init(null, request, response);
 
         resource.handle();