You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by re...@apache.org on 2012/11/20 21:19:30 UTC

svn commit: r1411845 - in /stanbol/trunk/commons/web/ldviewable/src/main/java/org/apache/stanbol/commons/ldviewable: Viewable.java mbw/ViewableWriter.java

Author: reto
Date: Tue Nov 20 20:19:29 2012
New Revision: 1411845

URL: http://svn.apache.org/viewvc?rev=1411845&view=rev
Log:
STANBOL-742: added near drop-in replacement for jersey viewable

Added:
    stanbol/trunk/commons/web/ldviewable/src/main/java/org/apache/stanbol/commons/ldviewable/Viewable.java
    stanbol/trunk/commons/web/ldviewable/src/main/java/org/apache/stanbol/commons/ldviewable/mbw/ViewableWriter.java

Added: stanbol/trunk/commons/web/ldviewable/src/main/java/org/apache/stanbol/commons/ldviewable/Viewable.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/commons/web/ldviewable/src/main/java/org/apache/stanbol/commons/ldviewable/Viewable.java?rev=1411845&view=auto
==============================================================================
--- stanbol/trunk/commons/web/ldviewable/src/main/java/org/apache/stanbol/commons/ldviewable/Viewable.java (added)
+++ stanbol/trunk/commons/web/ldviewable/src/main/java/org/apache/stanbol/commons/ldviewable/Viewable.java Tue Nov 20 20:19:29 2012
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.stanbol.commons.ldviewable;
+
+
+/**
+ * This is a replacement for the jersey Vieable that allows rendering an 
+ * arbitrary object using a Freemarker template specified by path.
+ * 
+ * Usage of this class promotes a bad programming style where the 
+ * application logic is not clearly separated from the presentation but 
+ * where backend method are called by the presentation layer.
+ * 
+ * Users should consider migrate to LdViewable instead where instead of
+ * an arbitrary Object a GraphNode representing a node in a graph is passed,
+ * this approach also allows the response to be rendered as RDF.
+ *
+ */
+public class Viewable {
+
+	/**
+	 * This uses the class name of Pojo to prefix the template
+	 * 
+	 * @param templatePath the templatePath
+	 * @param graphNode the graphNode with the actual content
+	 */
+	public Viewable(final String templatePath, final Object pojo) {
+		this(templatePath, pojo, pojo.getClass());
+	}
+	
+	/**
+	 * With this version of the constructor the templatePath is prefixed with
+	 * the slash-separated class name of clazz.
+	 * 
+	 */
+	public Viewable(final String templatePath, final Object pojo, final Class<?> clazz) {
+		final String slahSeparatedPacakgeName = clazz.getName().replace('.', '/');
+		if (templatePath.startsWith("/")) {
+			this.templatePath = slahSeparatedPacakgeName+templatePath;
+		} else {
+			this.templatePath = slahSeparatedPacakgeName+'/'+templatePath;
+		}
+		this.pojo = pojo;
+	}
+	
+	private String templatePath;
+	private Object pojo;
+	
+	public String getTemplatePath() {
+		return templatePath;
+	}
+	
+	public Object getPojo() {
+		return pojo;
+	}
+}

Added: stanbol/trunk/commons/web/ldviewable/src/main/java/org/apache/stanbol/commons/ldviewable/mbw/ViewableWriter.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/commons/web/ldviewable/src/main/java/org/apache/stanbol/commons/ldviewable/mbw/ViewableWriter.java?rev=1411845&view=auto
==============================================================================
--- stanbol/trunk/commons/web/ldviewable/src/main/java/org/apache/stanbol/commons/ldviewable/mbw/ViewableWriter.java (added)
+++ stanbol/trunk/commons/web/ldviewable/src/main/java/org/apache/stanbol/commons/ldviewable/mbw/ViewableWriter.java Tue Nov 20 20:19:29 2012
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.stanbol.commons.ldviewable.mbw;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+
+import javax.ws.rs.Produces;
+import javax.ws.rs.WebApplicationException;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyWriter;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.stanbol.commons.ldpathtemplate.LdRenderer;
+import org.apache.stanbol.commons.ldviewable.Viewable;
+
+@Component
+@Service(ViewableWriter.class)
+@Produces("text/html")
+public class ViewableWriter implements MessageBodyWriter<Viewable> {
+
+	@Reference
+	private LdRenderer ldRenderer;
+	
+	@Override
+	public boolean isWriteable(Class<?> type, Type genericType,
+			Annotation[] annotations, MediaType mediaType) {
+		return Viewable.class.isAssignableFrom(type);
+	}
+
+	@Override
+	public long getSize(Viewable t, Class<?> type, Type genericType,
+			Annotation[] annotations, MediaType mediaType) {
+		return -1;
+	}
+
+	@Override
+	public void writeTo(final Viewable t, Class<?> type, Type genericType,
+			Annotation[] annotations, MediaType mediaType,
+			MultivaluedMap<String, Object> httpHeaders,
+			OutputStream entityStream) throws IOException,
+			WebApplicationException {
+		Writer out = new OutputStreamWriter(entityStream, "utf-8"); 
+		ldRenderer.renderPojo(new Wrapper(t.getPojo()), "html/"+t.getTemplatePath(), out);
+		out.flush();
+	}
+	static public class Wrapper {
+
+		private Object wrapped;
+		public Wrapper(Object wrapped) {
+			this.wrapped = wrapped;
+		}
+		public Object getIt() {
+			return wrapped;
+		}
+		
+		
+	}
+
+}