Author: reto
Date: Mon Mar 15 16:11:15 2010
New Revision: 923321
URL: http://svn.apache.org/viewvc?rev=923321&view=rev
Log:
CLEREZZA-96: a version with a rootLinkPrefix hack, should work in most cases
Modified:
incubator/clerezza/issues/CLEREZZA-96/org.apache.clerezza.tools.offline/pom.xml
incubator/clerezza/issues/CLEREZZA-96/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/Generator.java
Modified:
incubator/clerezza/issues/CLEREZZA-96/org.apache.clerezza.tools.offline/pom.xml
URL:
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-96/org.apache.clerezza.tools.offline/pom.xml?rev=923321&r1=923320&r2=923321&view=diff
==============================================================================
---
incubator/clerezza/issues/CLEREZZA-96/org.apache.clerezza.tools.offline/pom.xml
(original)
+++
incubator/clerezza/issues/CLEREZZA-96/org.apache.clerezza.tools.offline/pom.xml
Mon Mar 15 16:11:15 2010
@@ -55,5 +55,10 @@
<groupId>org.apache.clerezza</groupId>
<artifactId>org.apache.clerezza.rdf.web.ontologies</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.clerezza</groupId>
+
<artifactId>org.apache.clerezza.web.fileserver</artifactId>
+ </dependency>
</dependencies>
</project>
+
Modified:
incubator/clerezza/issues/CLEREZZA-96/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/Generator.java
URL:
http://svn.apache.org/viewvc/incubator/clerezza/issues/CLEREZZA-96/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/Generator.java?rev=923321&r1=923320&r2=923321&view=diff
==============================================================================
---
incubator/clerezza/issues/CLEREZZA-96/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/Generator.java
(original)
+++
incubator/clerezza/issues/CLEREZZA-96/org.apache.clerezza.tools.offline/src/main/java/org/apache/clerezza/tools/offline/Generator.java
Mon Mar 15 16:11:15 2010
@@ -18,12 +18,14 @@
*/
package org.apache.clerezza.tools.offline;
-import org.apache.clerezza.rdf.utils.UriMutatingTripleCollection;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
-import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
@@ -38,7 +40,6 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
import org.apache.clerezza.platform.graphprovider.content.ContentGraphProvider;
-import org.apache.clerezza.platform.typerendering.Renderer;
import org.apache.clerezza.platform.typerendering.RendererFactory;
import org.apache.clerezza.rdf.core.MGraph;
import org.apache.clerezza.rdf.core.NonLiteral;
@@ -52,16 +53,14 @@ import org.apache.felix.scr.annotations.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.clerezza.rdf.core.serializedform.Serializer;
-import org.apache.clerezza.rdf.utils.GraphNode;
+import org.apache.clerezza.web.fileserver.util.MediaTypeGuesser;
import org.wymiwyg.commons.util.dirbrowser.PathNode;
/**
* This JAX-RS resource provides a method to retrieve a zip file containing
- * all triple collections that the use may access. The triple collection are
- * serialized in N-Triples format. The URI path of this resource is
- * "/admin/backup".
+ * an offline version of a site served by the clerezza instance
*
- * @author hasan
+ * @author reto
*/
@Component
@Service(Object.class)
@@ -78,8 +77,12 @@ public class Generator {
@Reference
private RendererFactory rendererFactory;
+ private MediaTypeGuesser mediaTypeGuesser =
MediaTypeGuesser.getInstance();
+
final Logger logger = LoggerFactory.getLogger(Generator.class);
+ final private Charset UTF8 = Charset.forName("utf-8");
+
/**
*
* The service takes the following argumens:
@@ -107,8 +110,24 @@ public class Generator {
public Response download(@QueryParam("baseUri") String baseUri,
@QueryParam("targetUri") String targetUri,
@QueryParam("rootLinkPrefix") String rootLinkPrefix,
- @QueryParam("formatExtensions") List<String>
formatExtensions) throws IOException {
+ @QueryParam("formatExtension") List<String>
formatExtensions) throws IOException {
+ if (baseUri == null) {
+ throw new WebApplicationException(Response.
+ status(Status.BAD_REQUEST).entity(
+ "Parameter baseUri missing").build());
+ }
+ if (targetUri == null) {
+ targetUri = baseUri;
+ }
+ if (targetUri == null) {
+ rootLinkPrefix = "";
+ }
+ if (formatExtensions == null) {
+ throw new WebApplicationException(Response.
+ status(Status.BAD_REQUEST).entity(
+ "Parameter formatExtension missing, at
least one required").build());
+ }
byte[] byteArray = createOfflineSite(baseUri, targetUri,
rootLinkPrefix,
formatExtensions);
ResponseBuilder responseBuilder = Response.status(Status.OK).
@@ -142,21 +161,59 @@ public class Generator {
}
}
for (UriRef uriRef : matchingUri) {
- generateFileForResource(baseUri, targetUri,
- rootLinkPrefix, uriRef, contentGraph,
result);
+ generateFilesForResource(baseUri, targetUri,
+ rootLinkPrefix, uriRef, contentGraph,
formatExtensions,
+ result);
}
return result;
}
- private void generateFileForResource(String baseUri, String
targetBaseUri,
+ /**
+ * Currently not using graph, but in future this might be used for
special
+ * handling of infodicscobits
+ */
+ private void generateFilesForResource(String baseUri, String
targetBaseUri,
String rootLinkPrefix, UriRef uriRef, TripleCollection
graph,
- Hierarchy hierarchy) throws IOException {
+ List<String> formatExtensions, Hierarchy hierarchy)
throws IOException {
final String path = getPathForUriRef(uriRef, baseUri);
final UriRef targetUri = new UriRef(baseUri+path);
- final byte[] data = getGeneratedContent(targetUri,
- new UriMutatingTripleCollection(graph, baseUri,
targetBaseUri));
- final byte[] dataPrefixApplied = applyRootLinkPrefic(data,
rootLinkPrefix);
- hierarchy.addChild(path+".xhtml", dataPrefixApplied);
+ for (String formatExtension : formatExtensions) {
+ MediaType mediaType =
mediaTypeGuesser.getTypeForExtension(formatExtension);
+ try {
+ final byte[] variant = getVariant(uriRef,
mediaType);
+ final byte[] dataPrefixApplied =
applyRootLinkPrefic(variant,
+ rootLinkPrefix, mediaType);
+ final String filePath =
uriRef.getUnicodeString().endsWith("/") ? path+"index" : path;
+ final String dottedExtension =
"."+formatExtension;
+ final String extendedPath =
filePath.endsWith(dottedExtension) ?
+ filePath : filePath + dottedExtension;
+ hierarchy.addChild(extendedPath,
dataPrefixApplied);
+ } catch (VariantUnavailableException ex) {
+ logger.info("{} not available as {}", uriRef,
mediaType);
+ }
+ }
+ }
+
+ private byte[] getVariant(UriRef uriRef, MediaType mediaType) throws
+ IOException, VariantUnavailableException {
+ final URL url = new URL(uriRef.getUnicodeString());
+ final HttpURLConnection urlConnection = (HttpURLConnection)
url.openConnection();
+ urlConnection.setRequestProperty("Accept",
mediaType.toString());
+ urlConnection.connect();
+ final int responseCode = urlConnection.getResponseCode();
+ if (responseCode != 200) {
+ throw new VariantUnavailableException("response code:
"+responseCode);
+ }
+ final String responseContentType =
urlConnection.getContentType();
+ if (!responseContentType.startsWith(mediaType.toString())) {
+ throw new VariantUnavailableException("Got
"+responseContentType+" and not "+mediaType);
+ }
+ final InputStream in = urlConnection.getInputStream();
+ final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ for (int ch = in.read(); ch != -1; ch = in.read()) {
+ baos.write(ch);
+ }
+ return baos.toByteArray();
}
private String getPathForUriRef(UriRef uriRef, String baseUri) {
@@ -169,20 +226,31 @@ public class Generator {
return dateFormat.format(date);
}
- private byte[] getGeneratedContent(UriRef resource, TripleCollection
graph) throws IOException {
- GraphNode docRootNode = new GraphNode(resource, graph);
- Renderer renderer = rendererFactory.createRenderer(docRootNode,
null,
-
Collections.singletonList(MediaType.APPLICATION_XHTML_XML_TYPE));
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- renderer.render(docRootNode, docRootNode, baos);
- return baos.toByteArray();
+
+ private Set<String> rootLinkIndicators = new HashSet<String>();
+ {
+ rootLinkIndicators.add("src=\"");
+ rootLinkIndicators.add("href=\"");
+ rootLinkIndicators.add("url(");
}
- private byte[] applyRootLinkPrefic(byte[] data, String rootLinkPrefix) {
- //TODO impl
- return data;
+ private byte[] applyRootLinkPrefic(byte[] variant, String
rootLinkPrefix,
+ MediaType mediaType) {
+ //here we should locate some mediaType specific handlers
+ //a quick hack
+ String stringVersion = new String(variant, UTF8);
+ for (String rootLinkIndicator : rootLinkIndicators) {
+ stringVersion =
stringVersion.replaceAll(rootLinkIndicator+"/",
+ rootLinkIndicator+rootLinkPrefix+"/");
+ }
+ return stringVersion.getBytes(UTF8);
}
+ private static class VariantUnavailableException extends Exception {
+ VariantUnavailableException(String message) {
+ super(message);
+ }
+ }
}