You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by wi...@apache.org on 2013/04/11 13:24:32 UTC
[1/2] git commit: MARMOTTA-194: refactorized templating engine
Updated Branches:
refs/heads/master 1e974a014 -> 6059fb835
MARMOTTA-194: refactorized templating engine
Project: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/commit/6c6288f4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/tree/6c6288f4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/diff/6c6288f4
Branch: refs/heads/master
Commit: 6c6288f4c77d9bcdfefcda3cf7d7e6610c9db2cb
Parents: 1e974a0
Author: Sergio Fernández <wi...@apache.org>
Authored: Thu Apr 11 13:17:05 2013 +0200
Committer: Sergio Fernández <wi...@apache.org>
Committed: Thu Apr 11 13:17:05 2013 +0200
----------------------------------------------------------------------
.../core/api/templating/AdminInterfaceService.java | 55 +++
.../core/api/templating/TemplatingService.java | 58 ++--
.../platform/core/filters/TemplatingFilter.java | 27 +-
.../platform/core/rio/RDFHtmlWriterImpl.java | 16 +-
.../templating/AdminTemplatingServiceImpl.java | 283 ++++++++++++
.../core/services/templating/TemplatingHelper.java | 93 ----
.../services/templating/TemplatingServiceImpl.java | 348 ++++-----------
.../webservices/resource/ContentWebService.java | 14 +-
.../webservices/resource/InspectionWebService.java | 80 ++--
.../core/webservices/resource/MetaWebService.java | 18 +-
.../webservices/resource/ResourceWebService.java | 54 ++-
.../resource/ResourceWebServiceHelper.java | 20 +-
12 files changed, 579 insertions(+), 487 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/6c6288f4/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/api/templating/AdminInterfaceService.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/api/templating/AdminInterfaceService.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/api/templating/AdminInterfaceService.java
new file mode 100644
index 0000000..d60e9b4
--- /dev/null
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/api/templating/AdminInterfaceService.java
@@ -0,0 +1,55 @@
+/**
+ * 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.marmotta.platform.core.api.templating;
+
+import org.apache.marmotta.platform.core.exception.TemplatingException;
+
+import javax.servlet.ServletContext;
+
+/**
+ * User: Thomas Kurz
+ * Date: 22.07.11
+ * Time: 13:06
+ */
+public interface AdminInterfaceService {
+
+ public final static String PATH = "/templates/";
+
+ /**
+ * inits a freebase template service with a servlet context
+ * @param context
+ */
+ public void init(ServletContext context) throws TemplatingException;
+
+ /**
+ * this method wraps a file with a default template
+ * @param bytes
+ * @return
+ */
+ public byte[] process(byte[] bytes, String path) throws TemplatingException;
+
+ /**
+ * Check whether the templating service considers the resource passed in the path as a menu entry it is
+ * responsible for.
+ *
+ * @param path
+ * @return
+ */
+ boolean isMenuEntry(String path);
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/6c6288f4/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/api/templating/TemplatingService.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/api/templating/TemplatingService.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/api/templating/TemplatingService.java
index a1195b4..2e6c770 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/api/templating/TemplatingService.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/api/templating/TemplatingService.java
@@ -17,39 +17,45 @@
*/
package org.apache.marmotta.platform.core.api.templating;
-import org.apache.marmotta.platform.core.exception.TemplatingException;
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Map;
-import javax.servlet.ServletContext;
+import freemarker.template.Configuration;
+import freemarker.template.Template;
+import freemarker.template.TemplateException;
/**
- * User: Thomas Kurz
- * Date: 22.07.11
- * Time: 13:06
+ * Templating Service
+ *
+ * @author Sergio Fernández
*/
public interface TemplatingService {
- public final static String PATH = "/templates/";
+ final static String PATH = "/templates/";
+
+ static final String DEFAULT_PROJECT = "marmotta";
+
+ static final String DEFAULT_STYLE = "blue";
+
+ void initDataModel();
+
+ Configuration getConfiguration();
+
+ Configuration getConfiguration(Class<?> cls);
+
+ Template getTemplate(String name) throws IOException;
+
+ Template getTemplate(Class<?> cls, String name) throws IOException;
+
+ String process(String name) throws IOException, TemplateException;
+
+ String process(String name, Map<String, Object> data) throws IOException, TemplateException;
- /**
- * inits a freebase template service with a servlet context
- * @param context
- */
- public void init(ServletContext context) throws TemplatingException;
-
- /**
- * this method wraps a file with a default template
- * @param bytes
- * @return
- */
- public byte[] process(byte[] bytes, String path) throws TemplatingException;
+ void process(String name, Writer writer) throws IOException, TemplateException;
+
+ void process(String name, Map<String, Object> data, Writer writer) throws IOException, TemplateException;
- /**
- * Check whether the templating service considers the resource passed in the path as a menu entry it is
- * responsible for.
- *
- * @param path
- * @return
- */
- boolean isMenuEntry(String path);
+ void process(Class<?> cls, String name, Map<String, Object> data, Writer writer) throws IOException, TemplateException;
}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/6c6288f4/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/filters/TemplatingFilter.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/filters/TemplatingFilter.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/filters/TemplatingFilter.java
index 6e78be3..4c3e541 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/filters/TemplatingFilter.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/filters/TemplatingFilter.java
@@ -17,13 +17,8 @@
*/
package org.apache.marmotta.platform.core.filters;
-import org.apache.marmotta.platform.core.api.config.ConfigurationService;
-import org.apache.marmotta.platform.core.api.modules.MarmottaHttpFilter;
-import org.apache.marmotta.platform.core.api.modules.MarmottaResourceService;
-import org.apache.marmotta.platform.core.api.modules.ResourceEntry;
-import org.apache.marmotta.platform.core.api.templating.TemplatingService;
-import org.apache.marmotta.platform.core.exception.TemplatingException;
-import org.slf4j.Logger;
+import java.io.IOException;
+import java.net.URL;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
@@ -34,8 +29,14 @@ import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import java.io.IOException;
-import java.net.URL;
+
+import org.apache.marmotta.platform.core.api.config.ConfigurationService;
+import org.apache.marmotta.platform.core.api.modules.MarmottaHttpFilter;
+import org.apache.marmotta.platform.core.api.modules.MarmottaResourceService;
+import org.apache.marmotta.platform.core.api.modules.ResourceEntry;
+import org.apache.marmotta.platform.core.api.templating.AdminInterfaceService;
+import org.apache.marmotta.platform.core.exception.TemplatingException;
+import org.slf4j.Logger;
/**
* Add file description here!
@@ -55,7 +56,7 @@ public class TemplatingFilter implements MarmottaHttpFilter {
private ConfigurationService configurationService;
@Inject
- private TemplatingService templatingService;
+ private AdminInterfaceService adminInterfaceService;
/**
* Return the pattern (regular expression) that a request URI (relative to the LMF base URI) has to match
@@ -94,7 +95,7 @@ public class TemplatingFilter implements MarmottaHttpFilter {
public void init(FilterConfig filterConfig) throws ServletException {
//init templating service
try {
- templatingService.init(filterConfig.getServletContext());
+ adminInterfaceService.init(filterConfig.getServletContext());
} catch (TemplatingException e) {
log.error("templating service could not be initialized: " + e.getMessage());
}
@@ -130,7 +131,7 @@ public class TemplatingFilter implements MarmottaHttpFilter {
long starttime = System.currentTimeMillis();
- if(path != null && templatingService.isMenuEntry(path)) {
+ if(path != null && adminInterfaceService.isMenuEntry(path)) {
ResourceEntry data = resourceService.getResource(path);
@@ -138,7 +139,7 @@ public class TemplatingFilter implements MarmottaHttpFilter {
if(data != null && data.getLength() > 0) {
try {
- byte [] templatedData = templatingService.process(data.getData(),path);
+ byte [] templatedData = adminInterfaceService.process(data.getData(),path);
data = new ResourceEntry(data.getLocation(),templatedData,templatedData.length,data.getContentType());
HttpServletResponse hresponse = (HttpServletResponse) response;
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/6c6288f4/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/rio/RDFHtmlWriterImpl.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/rio/RDFHtmlWriterImpl.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/rio/RDFHtmlWriterImpl.java
index 321dd22..92dc972 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/rio/RDFHtmlWriterImpl.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/rio/RDFHtmlWriterImpl.java
@@ -37,7 +37,7 @@ import org.apache.marmotta.platform.core.api.config.ConfigurationService;
import org.apache.marmotta.platform.core.api.io.RDFHtmlWriter;
import org.apache.marmotta.platform.core.api.io.RDFWriterPriority;
import org.apache.marmotta.platform.core.api.prefix.PrefixService;
-import org.apache.marmotta.platform.core.services.templating.TemplatingHelper;
+import org.apache.marmotta.platform.core.api.templating.TemplatingService;
import org.apache.marmotta.platform.core.util.CDIContext;
import org.openrdf.model.Literal;
import org.openrdf.model.Resource;
@@ -62,6 +62,8 @@ public class RDFHtmlWriterImpl implements RDFHtmlWriter {
protected ConfigurationService configurationService;
protected PrefixService prefixService;
+
+ protected TemplatingService templatingService;
protected Logger log = LoggerFactory.getLogger(RDFHtmlWriterImpl.class);
@@ -87,6 +89,7 @@ public class RDFHtmlWriterImpl implements RDFHtmlWriter {
configurationService = CDIContext
.getInstance(ConfigurationService.class);
prefixService = CDIContext.getInstance(PrefixService.class);
+ templatingService = CDIContext.getInstance(TemplatingService.class);
}
/**
@@ -214,21 +217,12 @@ public class RDFHtmlWriterImpl implements RDFHtmlWriter {
data.put("resources", resources);
data.put("prefixMappings", prefixService.serializePrefixMapping());
- data.put("SERVER_URL", configurationService.getServerUri());
- data.put("BASIC_URL", configurationService.getBaseUri());
- String project = configurationService.getStringConfiguration("kiwi.pages.project", "marmotta");
- data.put("PROJECT", project);
- data.put("LOGO", configurationService.getStringConfiguration("kiwi.pages.project."+project+".logo", project+".png"));
- data.put("CSS", configurationService.getStringConfiguration("kiwi.pages.project."+project+".css", "core/public/style/"+project+".css"));
- data.put("FOOTER", configurationService.getStringConfiguration("kiwi.pages.project."+project+".footer", "(footer not properly configured for project "+project+")"));
- data.put("DEFAULT_STYLE", configurationService.getStringConfiguration("kiwi.pages.style", "marmotta"));
-
//set timemap link
if(configurationService.getBooleanConfiguration("versioning.enabled")) {
data.put("timemaplink", configurationService.getStringConfiguration("versioning.memento.timemap"));
}
- TemplatingHelper.processTemplate(TEMPLATE, data, writer);
+ templatingService.process(TEMPLATE, data, writer);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new RDFHandlerException(e);
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/6c6288f4/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/templating/AdminTemplatingServiceImpl.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/templating/AdminTemplatingServiceImpl.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/templating/AdminTemplatingServiceImpl.java
new file mode 100644
index 0000000..2a0279d
--- /dev/null
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/templating/AdminTemplatingServiceImpl.java
@@ -0,0 +1,283 @@
+/**
+ * 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.marmotta.platform.core.services.templating;
+
+import java.io.ByteArrayOutputStream;
+import java.io.OutputStreamWriter;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+import javax.servlet.ServletContext;
+
+import org.apache.marmotta.platform.core.api.config.ConfigurationService;
+import org.apache.marmotta.platform.core.api.modules.ModuleService;
+import org.apache.marmotta.platform.core.api.templating.AdminInterfaceService;
+import org.apache.marmotta.platform.core.api.templating.TemplatingService;
+import org.apache.marmotta.platform.core.exception.TemplatingException;
+import org.apache.marmotta.platform.core.model.template.MenuItem;
+
+/**
+ * User: Thomas Kurz
+ * Date: 22.07.11
+ * Time: 13:06
+ */
+@ApplicationScoped
+public class AdminTemplatingServiceImpl implements AdminInterfaceService {
+
+ private ServletContext context;
+
+ private static enum Properties { HEAD, CONTENT }
+
+ @Inject
+ private ModuleService moduleService;
+
+ @Inject
+ private TemplatingService templatingService;
+
+ @Inject
+ private ConfigurationService configurationService;
+
+ //some statics
+ private static final String TEMPLATE_STRING = "admin.ftl";
+ private static final String DEFAULT_REST_PATH = "/doc/rest/";
+ private static final String DEFAULT_REST_FILE = "overview-summary.html";
+ private static final String DEFAULT_TITLE_FOR_WEBSERVICES = "webservices";
+
+
+ //pattern to filter comments content
+ private static final Pattern PATTERN = Pattern.compile("\\<!--###BEGIN_([^#]+)###--\\>(.+)\\<!--###END_\\1###--\\>",Pattern.DOTALL);
+ private Menu menu;
+
+ /**
+ * inits a freemarker template service with a servlet context
+ * @param context a servlet context
+ */
+ @Override
+ public void init(ServletContext context) throws TemplatingException {
+ menu = new Menu();
+ this.context = context;
+ try {
+ //try-run to check it from the very beginning
+ templatingService.getTemplate(TEMPLATE_STRING);
+ } catch (Exception e) {
+ throw new TemplatingException("Severe Error: admin template cannot be found: " + e.getMessage());
+ }
+ }
+
+ /**
+ * Check whether the templating service considers the resource passed in the path as a menu entry it is
+ * responsible for.
+ *
+ * @param path a url path
+ * @return if the give path points to an admin page
+ */
+ @Override
+ public boolean isMenuEntry(String path) {
+ if(menu.path_titles.keySet().contains(configurationService.getPath()+path)) return true;
+ if(path.contains(DEFAULT_REST_PATH)) return true;
+ else return false;
+ }
+
+ /**
+ * this method wraps a file with a specified admin template. If the file is not a admin page,
+ * the bytes are returned unprocessed
+ * @param bytes content represented in a byte array
+ * @return the processed (templated) byte array
+ */
+ @Override
+ public byte[] process(byte[] bytes, String path) throws TemplatingException {
+
+ if(!configurationService.getBooleanConfiguration("templating.cache.enabled",true) && context!=null) {
+ init(context);
+ }
+ //apply template
+ if(!isMenuEntry(path)) return bytes;
+ //activate
+ String module = menu.getCurrentModule(configurationService.getPath() + path);
+ //fill data model
+ Map<String, Object> datamodel = new HashMap<String,Object>();
+ for(Properties p : Properties.values()) {
+ datamodel.put(p.name(),"<!-- "+p.name()+" not defined -->");
+ }
+ //begin hack!!!
+ datamodel.put("USER_MODULE_IS_ACTIVE", moduleService.listModules().contains("Users"));
+ //end hack!!!
+ datamodel.put("MODULE_MENU",menu.menuItems);
+ datamodel.put("CURRENT_TITLE", getNameFromPath(path));
+ datamodel.put("CURRENT_MODULE", module);
+ try {
+ String s = new String(bytes);
+ Matcher m = PATTERN.matcher(s);
+ while (m.find()) {
+ datamodel.put(m.group(1),m.group(2));
+ }
+ } catch (Exception e) {
+ throw new TemplatingException(e.getMessage());
+ }
+
+ //make magic happen
+ try {
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ templatingService.process(TEMPLATE_STRING, datamodel, new OutputStreamWriter(bos));
+ bytes = bos.toByteArray();
+ bos.flush();
+ bos.close();
+ } catch (Exception e) {
+ throw new TemplatingException(e.getMessage());
+ }
+ return bytes;
+ }
+
+ /**
+ * This object represents a menu for the admin interface. It is build using the ModuleService.
+ */
+ class Menu {
+
+ public List<MenuItem> menuItems;
+ public Map<String,String> path_titles;
+
+ public Menu() {
+ //instantiate
+ menuItems = new ArrayList<MenuItem>();
+ path_titles = new HashMap<String, String>();
+ //sort menu
+ ArrayList<String> menuSorted = new ArrayList<String>(moduleService.listModules());
+ if(configurationService.getBooleanConfiguration("templating.sort_by_weight", true)) {
+ Collections.sort(menuSorted, new Comparator<String>() {
+ @Override
+ public int compare(String o1, String o2) {
+ final int w1 = moduleService.getWeight(o1), w2 = moduleService.getWeight(o2);
+ if (w1 == w2) return o1.compareTo(o2);
+ return w1 - w2;
+ }
+ });
+ } else {
+ Collections.sort(menuSorted);
+ }
+
+ //build structure
+ for(String module : menuSorted) {
+ String path = configurationService.getPath() + moduleService.getModuleWeb(module);
+ if(moduleHasAdminPages(module)) {
+ MenuItem menu_item = new MenuItem();
+ menu_item.getProperties().put("title",module);
+ menu_item.getProperties().put("baseurl",moduleService.getModuleConfiguration(module).getConfiguration().getString("baseurl","/"+module));
+ for(String page : moduleService.getAdminPages(module)) {
+ if(page.equals("")) {
+ continue;
+ }
+ MenuItem submenu = new MenuItem();
+ submenu.getProperties().put("title",getNameFromPath(page));
+ submenu.getProperties().put("path",path+page);
+ //test if it is active
+ menu_item.getSubmenu().add(submenu);
+ path_titles.put(path+page,page);
+ }
+ if(moduleService.getWebservices(module)!=null &&
+ !moduleService.getWebservices(module).isEmpty() &&
+ !moduleService.getWebservices(module).iterator().next().equals("")) {
+ MenuItem submenu = new MenuItem();
+ submenu.getProperties().put("title",DEFAULT_TITLE_FOR_WEBSERVICES);
+ submenu.getProperties().put("path",path+DEFAULT_REST_PATH+DEFAULT_REST_FILE);
+ //test if it is active
+ menu_item.getSubmenu().add(submenu);
+ path_titles.put(path+DEFAULT_REST_PATH+DEFAULT_REST_FILE,DEFAULT_TITLE_FOR_WEBSERVICES);
+ }
+ menuItems.add(menu_item);
+ }
+ }
+ }
+
+ /**
+ * get current module and set submenu to active
+ * @param path the current system path
+ * @return current module name
+ */
+ public String getCurrentModule(String path) {
+ String module = "";
+ boolean active = false;
+ //test with module and submenu must be active
+ for(MenuItem menuItem : menuItems) {
+ if(path.startsWith((String)menuItem.getProperties().get("baseurl"))) {
+ module = (String)menuItem.getProperties().get("title");
+ }
+ menuItem.getProperties().put("active",false);
+ for(MenuItem submenu : menuItem.getSubmenu()) {
+ if(submenu.getProperties().get("path").equals(path)) {
+ submenu.getProperties().put("active",true);
+ menuItem.getProperties().put("active",true);
+ module = (String)menuItem.getProperties().get("title");
+ active = true;
+ } else {
+ submenu.getProperties().put("active",false);
+ }
+ }
+ }
+ //workaround for webservices (autogenerated pages that are nit fix stored in the menu structure)
+ if(!active) {
+ for(MenuItem menuItem : menuItems) {
+ if(module.equals(menuItem.getProperties().get("title"))) {
+ for(MenuItem submenu : menuItem.getSubmenu()) {
+ if(submenu.getProperties().get("title").equals(DEFAULT_TITLE_FOR_WEBSERVICES)) {
+ submenu.getProperties().put("active",true);
+ menuItem.getProperties().put("active",true);
+ }
+ }
+ }
+ }
+ }
+ return module;
+ }
+
+ /**
+ * Tests if a module should appear in the menu
+ * @param module a module name
+ * @return true is module should appear, false if not
+ */
+ private boolean moduleHasAdminPages(String module) {
+ if(moduleService.getAdminPages(module)!=null &&
+ !moduleService.getAdminPages(module).isEmpty() &&
+ !moduleService.getAdminPages(module).get(0).equals(""))
+ return true;
+ else if(moduleService.getWebservices(module)!=null &&
+ !moduleService.getWebservices(module).isEmpty() &&
+ !moduleService.getWebservices(module).iterator().next().equals("")) return true;
+ return false;
+ }
+
+ }
+
+ /**
+ * returns a proper name for a path by using the filename.
+ * @param path
+ * @return
+ */
+ private String getNameFromPath(String path) {
+ if(path.contains(DEFAULT_REST_PATH)) return DEFAULT_TITLE_FOR_WEBSERVICES;
+ return path.substring(path.lastIndexOf("/")).replaceAll("/"," ").replaceAll("_"," ").replaceAll(".html","").replaceAll(".jsp","");
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/6c6288f4/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/templating/TemplatingHelper.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/templating/TemplatingHelper.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/templating/TemplatingHelper.java
deleted file mode 100644
index 04a888f..0000000
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/templating/TemplatingHelper.java
+++ /dev/null
@@ -1,93 +0,0 @@
-/**
- * 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.marmotta.platform.core.services.templating;
-
-import freemarker.template.Configuration;
-import freemarker.template.Template;
-import freemarker.template.TemplateException;
-import org.apache.marmotta.platform.core.api.templating.TemplatingService;
-
-import java.io.BufferedWriter;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.Writer;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * Some methods useful for template working
- *
- * @author Sergio Fernández
- *
- */
-public class TemplatingHelper {
-
- public static Configuration getConfiguration() {
- return getConfiguration(TemplatingHelper.class);
- }
-
- public static Configuration getConfiguration(Class<?> cls) {
- Configuration cfg = new Configuration();
- cfg.setClassForTemplateLoading(cls, TemplatingService.PATH);
- return cfg;
- }
-
- public static Template getTemplate(String name) throws IOException {
- return getConfiguration().getTemplate(name);
- }
-
- public static Template getTemplate(Class<?> cls, String name) throws IOException {
- return getConfiguration(cls).getTemplate(name);
- }
-
- public static Template getTemplate(Configuration conf, String name) throws IOException {
- return conf.getTemplate(name);
- }
-
- public static String processTemplate(String name) throws IOException, TemplateException {
- return processTemplate(name, new HashMap<String, Object>());
- }
-
- public static String processTemplate(String name, Map<String, Object> data) throws IOException, TemplateException {
- Template tpl = getTemplate(name);
- OutputStream os = new ByteArrayOutputStream();
- Writer writer = new BufferedWriter(new OutputStreamWriter(os));
- tpl.process(data, writer);
- writer.flush();
- return os.toString();
- }
-
- public static void processTemplate(String name, Writer writer) throws IOException, TemplateException {
- processTemplate(name, new HashMap<String, Object>(), writer);
- }
-
- public static void processTemplate(Class<?> cls, String name, Map<String, Object> data, Writer writer) throws IOException, TemplateException {
- Template tpl = getTemplate(cls, name);
- tpl.process(data, writer);
- writer.flush();
- }
-
- public static void processTemplate(String name, Map<String, Object> data, Writer writer) throws IOException, TemplateException {
- Template tpl = getTemplate(name);
- tpl.process(data, writer);
- writer.flush();
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/6c6288f4/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/templating/TemplatingServiceImpl.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/templating/TemplatingServiceImpl.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/templating/TemplatingServiceImpl.java
index 5e4fcb5..56728cc 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/templating/TemplatingServiceImpl.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/services/templating/TemplatingServiceImpl.java
@@ -17,157 +17,57 @@
*/
package org.apache.marmotta.platform.core.services.templating;
-import freemarker.template.Template;
-import org.apache.marmotta.platform.core.api.config.ConfigurationService;
-import org.apache.marmotta.platform.core.api.modules.ModuleService;
-import org.apache.marmotta.platform.core.api.templating.TemplatingService;
-import org.apache.marmotta.platform.core.events.ConfigurationChangedEvent;
-import org.apache.marmotta.platform.core.exception.TemplatingException;
-import org.apache.marmotta.platform.core.model.template.MenuItem;
+import java.io.BufferedWriter;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Map;
+import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
-import javax.servlet.ServletContext;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.OutputStreamWriter;
-import java.util.*;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
+import org.apache.marmotta.platform.core.api.config.ConfigurationService;
+import org.apache.marmotta.platform.core.api.templating.TemplatingService;
+import org.apache.marmotta.platform.core.events.ConfigurationChangedEvent;
+
+import freemarker.template.Configuration;
+import freemarker.template.Template;
+import freemarker.template.TemplateException;
/**
- * User: Thomas Kurz
- * Date: 22.07.11
- * Time: 13:06
+ * Template Service Implementation
+ *
+ * @author Sergio Fernández
*/
@ApplicationScoped
public class TemplatingServiceImpl implements TemplatingService {
-
- private ServletContext context;
-
- private static enum Properties { HEAD, CONTENT }
-
- @Inject
- private ModuleService moduleService;
-
+
+ private Map<String,String> common;
+
@Inject
private ConfigurationService configurationService;
- //some statics
- private static final String TEMPLATE_STRING = "admin.ftl";
- private static final String DEFAULT_REST_PATH = "/doc/rest/";
- private static final String DEFAULT_REST_FILE = "overview-summary.html";
- private static final String DEFAULT_STYLE = "blue";
- private static final String DEFAULT_TITLE_FOR_WEBSERVICES = "webservices";
- private static final String DEFAULT_PROJECT = "marmotta";
-
- //pattern to filter comments content
- private static final Pattern PATTERN = Pattern.compile("\\<!--###BEGIN_([^#]+)###--\\>(.+)\\<!--###END_\\1###--\\>",Pattern.DOTALL);
- private Template tpl;
- private Menu menu;
-
- /**
- * inits a freemarker template service with a servlet context
- * @param context a servlet context
- */
- @Override
- public void init(ServletContext context) throws TemplatingException {
- this.context = context;
- initDataModel();
- try {
- tpl = TemplatingHelper.getTemplate(TEMPLATE_STRING);
- } catch (IOException e) {
- //e.printStackTrace();
- throw new TemplatingException(e.getMessage());
- }
- }
-
- /**
- * Check whether the templating service considers the resource passed in the path as a menu entry it is
- * responsible for.
- *
- * @param path a url path
- * @return if the give path points to an admin page
- */
- @Override
- public boolean isMenuEntry(String path) {
- if(menu.path_titles.keySet().contains(configurationService.getPath()+path)) return true;
- if(path.contains(DEFAULT_REST_PATH)) return true;
- else return false;
- }
-
- /**
- * this method wraps a file with a specified admin template. If the file is not a admin page,
- * the bytes are returned unprocessed
- * @param bytes content represented in a byte array
- * @return the processed (templated) byte array
- */
- @Override
- public byte[] process(byte[] bytes, String path) throws TemplatingException {
-
- if(!configurationService.getBooleanConfiguration("templating.cache.enabled",true) && context!=null) {
- init(context);
- }
- //apply template
- if(!isMenuEntry(path)) return bytes;
- //activate
- String module = menu.getCurrentModule(configurationService.getPath() + path);
- //fill data model
- @SuppressWarnings("unchecked")
- Map<String, Object> datamodel = (Map<String, Object>)basic_map.clone();
- //begin hack!!!
- datamodel.put("USER_MODULE_IS_ACTIVE", moduleService.listModules().contains("Users"));
- //end hack!!!
- datamodel.put("MODULE_MENU",menu.menuItems);
- datamodel.put("DEFAULT_STYLE", configurationService.getStringConfiguration("kiwi.pages.style_path", DEFAULT_STYLE));
- datamodel.put("CURRENT_TITLE", getNameFromPath(path));
- datamodel.put("CURRENT_MODULE", module);
- try {
- String s = new String(bytes);
- Matcher m = PATTERN.matcher(s);
- while (m.find()) {
- datamodel.put(m.group(1),m.group(2));
- }
- } catch (Exception e) {
- throw new TemplatingException(e.getMessage());
- }
-
- //make magic happen
- try {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- tpl.process(datamodel, new OutputStreamWriter(bos));
- bytes = bos.toByteArray();
- bos.flush();
- bos.close();
- } catch (Exception e) {
- throw new TemplatingException(e.getMessage());
- }
- return bytes;
- }
+ public TemplatingServiceImpl() {
+ super();
+ common = new HashMap<String,String>();
+ }
- //datamodel may not be empty
- private HashMap<String,String> basic_map;
- /**
- * creates a data model, which contains all properties (with empty values)
- */
- private void initDataModel() {
- basic_map = new HashMap<String,String>();
- for(Properties p : Properties.values()) {
- basic_map.put(p.name(),"<!-- "+p.name()+" not defined -->");
- }
- basic_map.put("SERVER_URL", configurationService.getServerUri());
- basic_map.put("BASIC_URL", configurationService.getBaseUri());
+ @PostConstruct
+ public void initDataModel() {
String project = configurationService.getStringConfiguration("kiwi.pages.project", DEFAULT_PROJECT);
- basic_map.put("PROJECT", project);
- basic_map.put("LOGO", configurationService.getStringConfiguration("kiwi.pages.project."+project+".logo", project+".png"));
- basic_map.put("CSS", configurationService.getStringConfiguration("kiwi.pages.project."+project+".css", "core/public/style/"+project+".css"));
- basic_map.put("FOOTER", configurationService.getStringConfiguration("kiwi.pages.project."+project+".footer", "(footer not properly configured for project "+project+")"));
-
- menu = new Menu();
- }
-
+ common.put("PROJECT", project);
+ common.put("DEFAULT_STYLE", configurationService.getStringConfiguration("kiwi.pages.style_path", DEFAULT_STYLE));
+ common.put("SERVER_URL", configurationService.getServerUri());
+ common.put("BASIC_URL", configurationService.getBaseUri());
+ common.put("LOGO", configurationService.getStringConfiguration("kiwi.pages.project."+project+".logo", project+".png"));
+ common.put("FOOTER", configurationService.getStringConfiguration("kiwi.pages.project."+project+".footer", "(footer not properly configured for project "+project+")"));
+ }
+
/**
* Update the data model in case an important value has changed
* @param event
@@ -180,134 +80,58 @@ public class TemplatingServiceImpl implements TemplatingService {
initDataModel();
}
}
-
- /**
- * This object represents a menu for the admin interface. It is build using the ModuleService.
- */
- class Menu {
-
- public List<MenuItem> menuItems;
- public Map<String,String> path_titles;
-
- public Menu() {
- //instantiate
- menuItems = new ArrayList<MenuItem>();
- path_titles = new HashMap<String, String>();
- //sort menu
- ArrayList<String> menuSorted = new ArrayList<String>(moduleService.listModules());
- if(configurationService.getBooleanConfiguration("templating.sort_by_weight",true)) {
- Collections.sort(menuSorted, new Comparator<String>() {
- @Override
- public int compare(String o1, String o2) {
- final int w1 = moduleService.getWeight(o1), w2 = moduleService.getWeight(o2);
- if (w1 == w2) return o1.compareTo(o2);
- return w1 - w2;
- }
- });
- } else {
- Collections.sort(menuSorted);
- }
-
- //build structure
- for(String module : menuSorted) {
- String path = configurationService.getPath() + moduleService.getModuleWeb(module);
- if(moduleHasAdminPages(module)) {
- MenuItem menu_item = new MenuItem();
- menu_item.getProperties().put("title",module);
- menu_item.getProperties().put("baseurl",moduleService.getModuleConfiguration(module).getConfiguration().getString("baseurl","/"+module));
- for(String page : moduleService.getAdminPages(module)) {
- if(page.equals("")) {
- continue;
- }
- MenuItem submenu = new MenuItem();
- submenu.getProperties().put("title",getNameFromPath(page));
- submenu.getProperties().put("path",path+page);
- //test if it is active
- menu_item.getSubmenu().add(submenu);
- path_titles.put(path+page,page);
- }
- if(moduleService.getWebservices(module)!=null &&
- !moduleService.getWebservices(module).isEmpty() &&
- !moduleService.getWebservices(module).iterator().next().equals("")) {
- MenuItem submenu = new MenuItem();
- submenu.getProperties().put("title",DEFAULT_TITLE_FOR_WEBSERVICES);
- submenu.getProperties().put("path",path+DEFAULT_REST_PATH+DEFAULT_REST_FILE);
- //test if it is active
- menu_item.getSubmenu().add(submenu);
- path_titles.put(path+DEFAULT_REST_PATH+DEFAULT_REST_FILE,DEFAULT_TITLE_FOR_WEBSERVICES);
- }
- menuItems.add(menu_item);
- }
- }
- }
-
- /**
- * get current module and set submenu to active
- * @param path the current system path
- * @return current module name
- */
- public String getCurrentModule(String path) {
- String module = "";
- boolean active = false;
- //test with module and submenu must be active
- for(MenuItem menuItem : menuItems) {
- if(path.startsWith((String)menuItem.getProperties().get("baseurl"))) {
- module = (String)menuItem.getProperties().get("title");
- }
- menuItem.getProperties().put("active",false);
- for(MenuItem submenu : menuItem.getSubmenu()) {
- if(submenu.getProperties().get("path").equals(path)) {
- submenu.getProperties().put("active",true);
- menuItem.getProperties().put("active",true);
- module = (String)menuItem.getProperties().get("title");
- active = true;
- } else {
- submenu.getProperties().put("active",false);
- }
- }
- }
- //workaround for webservices (autogenerated pages that are nit fix stored in the menu structure)
- if(!active) {
- for(MenuItem menuItem : menuItems) {
- if(module.equals(menuItem.getProperties().get("title"))) {
- for(MenuItem submenu : menuItem.getSubmenu()) {
- if(submenu.getProperties().get("title").equals(DEFAULT_TITLE_FOR_WEBSERVICES)) {
- submenu.getProperties().put("active",true);
- menuItem.getProperties().put("active",true);
- }
- }
- }
- }
- }
- return module;
- }
-
- /**
- * Tests if a module should appear in the menu
- * @param module a module name
- * @return true is module should appear, false if not
- */
- private boolean moduleHasAdminPages(String module) {
- if(moduleService.getAdminPages(module)!=null &&
- !moduleService.getAdminPages(module).isEmpty() &&
- !moduleService.getAdminPages(module).get(0).equals(""))
- return true;
- else if(moduleService.getWebservices(module)!=null &&
- !moduleService.getWebservices(module).isEmpty() &&
- !moduleService.getWebservices(module).iterator().next().equals("")) return true;
- return false;
- }
-
+
+ @Override
+ public Configuration getConfiguration() {
+ return getConfiguration(TemplatingServiceImpl.class);
}
- /**
- * returns a proper name for a path by using the filename.
- * @param path
- * @return
- */
- private String getNameFromPath(String path) {
- if(path.contains(DEFAULT_REST_PATH)) return DEFAULT_TITLE_FOR_WEBSERVICES;
- return path.substring(path.lastIndexOf("/")).replaceAll("/"," ").replaceAll("_"," ").replaceAll(".html","").replaceAll(".jsp","");
+ @Override
+ public Configuration getConfiguration(Class<?> cls) {
+ Configuration cfg = new Configuration();
+ cfg.setClassForTemplateLoading(cls, TemplatingService.PATH);
+ return cfg;
}
+ @Override
+ public Template getTemplate(String name) throws IOException {
+ return getConfiguration().getTemplate(name);
+ }
+
+ @Override
+ public Template getTemplate(Class<?> cls, String name) throws IOException {
+ return getConfiguration(cls).getTemplate(name);
+ }
+
+ @Override
+ public String process(String name) throws IOException, TemplateException {
+ return process(name, new HashMap<String, Object>());
+ }
+
+ @Override
+ public String process(String name, Map<String, Object> data) throws IOException, TemplateException {
+ OutputStream os = new ByteArrayOutputStream();
+ Writer writer = new BufferedWriter(new OutputStreamWriter(os));
+ process(name, data, writer);
+ return os.toString();
+ }
+
+ @Override
+ public void process(String name, Writer writer) throws IOException, TemplateException {
+ process(name, new HashMap<String, Object>(), writer);
+ }
+
+ @Override
+ public void process(String name, Map<String, Object> data, Writer writer) throws IOException, TemplateException {
+ process(TemplatingServiceImpl.class, name, data, writer);
+ }
+
+ @Override
+ public void process(Class<?> cls, String name, Map<String, Object> data, Writer writer) throws IOException, TemplateException {
+ Template tpl = getTemplate(cls, name);
+ data.putAll(common);
+ tpl.process(data, writer);
+ writer.flush();
+ }
+
}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/6c6288f4/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ContentWebService.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ContentWebService.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ContentWebService.java
index bbc13ce..7013767 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ContentWebService.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ContentWebService.java
@@ -44,6 +44,7 @@ import javax.ws.rs.core.Response.Status;
import org.apache.marmotta.platform.core.api.config.ConfigurationService;
import org.apache.marmotta.platform.core.api.content.ContentService;
import org.apache.marmotta.platform.core.api.io.MarmottaIOService;
+import org.apache.marmotta.platform.core.api.templating.TemplatingService;
import org.apache.marmotta.platform.core.api.triplestore.SesameService;
import org.apache.marmotta.platform.core.events.ContentCreatedEvent;
import org.apache.marmotta.platform.core.exception.MarmottaException;
@@ -68,6 +69,9 @@ public class ContentWebService {
@Inject
private ConfigurationService configurationService;
+
+ @Inject
+ private TemplatingService templatingService;
@Inject
private ContentService contentService;
@@ -260,7 +264,7 @@ public class ContentWebService {
if (resource != null) {
if (contentService.deleteContent(resource)) return Response.ok().build();
else
- return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Response.Status.NOT_FOUND, "no content found for this resource in LMF right now, but may be available again in the future", configurationService);
+ return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Response.Status.NOT_FOUND, "no content found for this resource in LMF right now, but may be available again in the future", configurationService, templatingService);
}
return Response.status(Response.Status.NOT_FOUND).build();
} finally {
@@ -352,7 +356,7 @@ public class ContentWebService {
}
return response;
} else {
- Response response = ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Status.NOT_ACCEPTABLE, "no content for mimetype " + mimetype, configurationService);
+ Response response = ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Status.NOT_ACCEPTABLE, "no content for mimetype " + mimetype, configurationService, templatingService);
ResourceWebServiceHelper.addHeader(response, "Content-Type", ResourceWebServiceHelper.appendContentTypes(contentService.getContentType(resource)));
return response;
}
@@ -385,16 +389,16 @@ public class ContentWebService {
public Response putContent(URI resource, String mimetype, HttpServletRequest request) {
try {
if (request.getContentLength() == 0)
- return ResourceWebServiceHelper.buildErrorPage(resource.stringValue(), configurationService.getBaseUri(), Status.BAD_REQUEST, "content may not be empty for writting", configurationService);
+ return ResourceWebServiceHelper.buildErrorPage(resource.stringValue(), configurationService.getBaseUri(), Status.BAD_REQUEST, "content may not be empty for writting", configurationService, templatingService);
contentService.setContentStream(resource, request.getInputStream(), mimetype); // store content
if(configurationService.getBooleanConfiguration(ENHANCER_STANBOL_ENHANCER_ENABLED, false)) {
afterContentCreated.fire(new ContentCreatedEvent(resource)); //enhancer
}
return Response.ok().build();
} catch (IOException e) {
- return ResourceWebServiceHelper.buildErrorPage(resource.stringValue(), configurationService.getBaseUri(), Status.BAD_REQUEST, "could not read request body", configurationService);
+ return ResourceWebServiceHelper.buildErrorPage(resource.stringValue(), configurationService.getBaseUri(), Status.BAD_REQUEST, "could not read request body", configurationService, templatingService);
} catch (WritingNotSupportedException e) {
- return ResourceWebServiceHelper.buildErrorPage(resource.stringValue(), configurationService.getBaseUri(), Status.FORBIDDEN, "writting this content is not supported", configurationService);
+ return ResourceWebServiceHelper.buildErrorPage(resource.stringValue(), configurationService.getBaseUri(), Status.FORBIDDEN, "writting this content is not supported", configurationService, templatingService);
}
}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/6c6288f4/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/InspectionWebService.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/InspectionWebService.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/InspectionWebService.java
index 88a25ab..56a0c98 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/InspectionWebService.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/InspectionWebService.java
@@ -17,35 +17,48 @@
*/
package org.apache.marmotta.platform.core.webservices.resource;
+import static org.apache.marmotta.commons.sesame.repository.ExceptionUtils.handleRepositoryException;
import static org.apache.marmotta.commons.sesame.repository.ResourceUtils.getAnonResource;
import static org.apache.marmotta.commons.sesame.repository.ResourceUtils.getLabel;
import static org.apache.marmotta.commons.sesame.repository.ResourceUtils.getUriResource;
import static org.apache.marmotta.commons.sesame.repository.ResourceUtils.listOutgoing;
-import static org.apache.marmotta.commons.sesame.repository.ExceptionUtils.handleRepositoryException;
-import org.apache.marmotta.platform.core.api.config.ConfigurationService;
-import org.apache.marmotta.platform.core.api.content.ContentService;
-import org.apache.marmotta.platform.core.api.triplestore.SesameService;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import javax.inject.Inject;
+import javax.ws.rs.DefaultValue;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
import org.apache.marmotta.commons.sesame.model.Namespaces;
import org.apache.marmotta.kiwi.model.rdf.KiWiResource;
import org.apache.marmotta.kiwi.model.rdf.KiWiTriple;
-import org.openrdf.model.*;
+import org.apache.marmotta.platform.core.api.config.ConfigurationService;
+import org.apache.marmotta.platform.core.api.triplestore.SesameService;
+import org.openrdf.model.BNode;
+import org.openrdf.model.Literal;
+import org.openrdf.model.Resource;
+import org.openrdf.model.Statement;
+import org.openrdf.model.URI;
+import org.openrdf.model.Value;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.RepositoryResult;
-import javax.inject.Inject;
-import javax.ws.rs.*;
-import javax.ws.rs.core.Response;
-import javax.ws.rs.core.Response.Status;
-
-import java.io.ByteArrayOutputStream;
-import java.io.PrintStream;
-import java.io.UnsupportedEncodingException;
-import java.net.URLEncoder;
-import java.util.*;
-
@Path("/" + ConfigurationService.INSPECT_PATH)
public class InspectionWebService {
@@ -57,10 +70,7 @@ public class InspectionWebService {
private ConfigurationService configurationService;
@Inject
- private SesameService sesameService;
-
- @Inject
- private ContentService contentService;
+ private SesameService sesameService;
@GET
@Path("/subject")
@@ -502,21 +512,21 @@ public class InspectionWebService {
return "UNKNOWN";
}
- private String buildResourceLink(RepositoryConnection conn, URI resource, String rel, String mime) {
- final String src = configurationService.getServerUri(), base = configurationService.getBaseUri();
-
- if (src.equals(base) && resource.toString().startsWith(base + ConfigurationService.RESOURCE_PATH + "/")) {
- final String uuid;
- uuid = resource.toString().substring((base + ConfigurationService.RESOURCE_PATH + "/").length());
- return String.format("%s%s/%s/%s", base, rel, mime, uuid);
- } else {
- try {
- return String.format("%s%s/%s?uri=%s", src, rel, mime, URLEncoder.encode(resource.toString(), CHARSET));
- } catch (UnsupportedEncodingException e) {
- return String.format("%s%s/%s?uri=%s", src, rel, mime, resource.toString());
- }
- }
- }
+// private String buildResourceLink(RepositoryConnection conn, URI resource, String rel, String mime) {
+// final String src = configurationService.getServerUri(), base = configurationService.getBaseUri();
+//
+// if (src.equals(base) && resource.toString().startsWith(base + ConfigurationService.RESOURCE_PATH + "/")) {
+// final String uuid;
+// uuid = resource.toString().substring((base + ConfigurationService.RESOURCE_PATH + "/").length());
+// return String.format("%s%s/%s/%s", base, rel, mime, uuid);
+// } else {
+// try {
+// return String.format("%s%s/%s?uri=%s", src, rel, mime, URLEncoder.encode(resource.toString(), CHARSET));
+// } catch (UnsupportedEncodingException e) {
+// return String.format("%s%s/%s?uri=%s", src, rel, mime, resource.toString());
+// }
+// }
+// }
private String createInfo(long id) {
StringBuilder b = new StringBuilder();
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/6c6288f4/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/MetaWebService.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/MetaWebService.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/MetaWebService.java
index a09a9e2..022fb31 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/MetaWebService.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/MetaWebService.java
@@ -46,6 +46,7 @@ import org.apache.marmotta.commons.sesame.repository.ResourceUtils;
import org.apache.marmotta.platform.core.api.config.ConfigurationService;
import org.apache.marmotta.platform.core.api.content.ContentService;
import org.apache.marmotta.platform.core.api.io.MarmottaIOService;
+import org.apache.marmotta.platform.core.api.templating.TemplatingService;
import org.apache.marmotta.platform.core.api.triplestore.ContextService;
import org.apache.marmotta.platform.core.api.triplestore.SesameService;
import org.apache.marmotta.platform.core.services.sesame.KiWiSesameUtil;
@@ -76,6 +77,9 @@ public class MetaWebService {
private ConfigurationService configurationService;
@Inject
+ private TemplatingService templatingService;
+
+ @Inject
private ContextService contextService;
@Inject
@@ -216,7 +220,7 @@ public class MetaWebService {
}
} catch (RepositoryException e) {
- return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Status.INTERNAL_SERVER_ERROR, e.getMessage(), configurationService);
+ return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Status.INTERNAL_SERVER_ERROR, e.getMessage(), configurationService, templatingService);
}
}
@@ -242,7 +246,7 @@ public class MetaWebService {
RepositoryConnection conn = sesameService.getConnection();
if (!ResourceUtils.existsResource(conn, uri)) {
- return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Response.Status.NOT_FOUND, "the requested resource could not be found in LMF right now, but may be available again in the future", configurationService);
+ return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Response.Status.NOT_FOUND, "the requested resource could not be found in LMF right now, but may be available again in the future", configurationService, templatingService);
}
try {
@@ -330,7 +334,7 @@ public class MetaWebService {
}
}
} catch (RepositoryException e) {
- return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Status.INTERNAL_SERVER_ERROR, e.getMessage(), configurationService);
+ return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Status.INTERNAL_SERVER_ERROR, e.getMessage(), configurationService, templatingService);
}
}
@@ -344,7 +348,7 @@ public class MetaWebService {
return response;
}
if (request.getContentLength() == 0)
- return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Status.BAD_REQUEST, "content may not be empty in resource update", configurationService);
+ return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Status.BAD_REQUEST, "content may not be empty in resource update", configurationService, templatingService);
// a intercepting connection that filters out all triples that have
// the wrong subject
@@ -367,11 +371,11 @@ public class MetaWebService {
}
return Response.ok().build();
} catch (RepositoryException e) {
- return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Status.INTERNAL_SERVER_ERROR, e.getMessage(), configurationService);
+ return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Status.INTERNAL_SERVER_ERROR, e.getMessage(), configurationService, templatingService);
} catch (IOException e) {
- return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Status.INTERNAL_SERVER_ERROR, "could not read request body", configurationService);
+ return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Status.INTERNAL_SERVER_ERROR, "could not read request body", configurationService, templatingService);
} catch (RDFParseException e) {
- return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Status.UNSUPPORTED_MEDIA_TYPE, "could not parse request body", configurationService);
+ return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Status.UNSUPPORTED_MEDIA_TYPE, "could not parse request body", configurationService, templatingService);
}
}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/6c6288f4/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebService.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebService.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebService.java
index 415fd25..1ad5512 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebService.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebService.java
@@ -19,38 +19,47 @@ package org.apache.marmotta.platform.core.webservices.resource;
import static javax.ws.rs.core.Response.status;
+import java.io.UnsupportedEncodingException;
+import java.net.URISyntaxException;
+import java.net.URLDecoder;
+import java.util.Collections;
+import java.util.List;
+import java.util.UUID;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.HeaderParam;
+import javax.ws.rs.OPTIONS;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.ResponseBuilder;
+import javax.ws.rs.core.Response.Status;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.marmotta.commons.http.ContentType;
+import org.apache.marmotta.commons.http.ETagGenerator;
+import org.apache.marmotta.commons.http.LMFHttpUtils;
import org.apache.marmotta.commons.sesame.repository.ResourceUtils;
import org.apache.marmotta.platform.core.api.config.ConfigurationService;
import org.apache.marmotta.platform.core.api.content.ContentService;
import org.apache.marmotta.platform.core.api.io.MarmottaIOService;
+import org.apache.marmotta.platform.core.api.templating.TemplatingService;
import org.apache.marmotta.platform.core.api.triplestore.SesameService;
import org.apache.marmotta.platform.core.services.sesame.KiWiSesameUtil;
-import org.apache.commons.lang.StringUtils;
-import org.apache.marmotta.commons.http.ContentType;
-import org.apache.marmotta.commons.http.ETagGenerator;
-import org.apache.marmotta.commons.http.LMFHttpUtils;
import org.openrdf.model.Resource;
import org.openrdf.model.URI;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.slf4j.Logger;
-import javax.enterprise.context.ApplicationScoped;
-import javax.inject.Inject;
-import javax.servlet.http.HttpServletRequest;
-import javax.ws.rs.*;
-import javax.ws.rs.core.Context;
-import javax.ws.rs.core.Response;
-import javax.ws.rs.core.Response.ResponseBuilder;
-import javax.ws.rs.core.Response.Status;
-
-import java.io.UnsupportedEncodingException;
-import java.net.URISyntaxException;
-import java.net.URLDecoder;
-import java.util.Collections;
-import java.util.List;
-import java.util.UUID;
-
/**
* Resource Web Services
*
@@ -72,6 +81,9 @@ public class ResourceWebService {
@Inject
private ConfigurationService configurationService;
+
+ @Inject
+ private TemplatingService templatingService;
@Inject
private ContentService contentService;
@@ -354,7 +366,7 @@ public class ResourceWebService {
log.debug("So redirecting directly to it...");
return Response.seeOther(new java.net.URI(uri)).build();
}
- if (resource == null) return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Response.Status.NOT_FOUND, "the requested resource could not be found in LMF right now, but may be available again in the future", configurationService);
+ if (resource == null) return ResourceWebServiceHelper.buildErrorPage(uri, configurationService.getBaseUri(), Response.Status.NOT_FOUND, "the requested resource could not be found in LMF right now, but may be available again in the future", configurationService, templatingService);
// FIXME String appendix = uuid == null ? "?uri=" + URLEncoder.encode(uri, "utf-8") :
// "/" + uuid;
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/6c6288f4/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebServiceHelper.java
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebServiceHelper.java b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebServiceHelper.java
index 88bbddd..1dd21cf 100644
--- a/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebServiceHelper.java
+++ b/platform/marmotta-core/src/main/java/org/apache/marmotta/platform/core/webservices/resource/ResourceWebServiceHelper.java
@@ -18,7 +18,7 @@
package org.apache.marmotta.platform.core.webservices.resource;
import org.apache.marmotta.platform.core.api.config.ConfigurationService;
-import org.apache.marmotta.platform.core.services.templating.TemplatingHelper;
+import org.apache.marmotta.platform.core.api.templating.TemplatingService;
import org.apache.marmotta.commons.http.ContentType;
import org.openrdf.model.URI;
@@ -38,7 +38,9 @@ import java.util.Map;
*/
public class ResourceWebServiceHelper {
- public static void addHeader(Response response, String name, String value) {
+ private static final String TEMPLATE_404 = "404.ftl";
+
+ public static void addHeader(Response response, String name, String value) {
response.getMetadata().add(name, value);
}
@@ -144,9 +146,8 @@ public class ResourceWebServiceHelper {
}
}
- public static Response buildErrorPage(String uri, String base, Status status, String message, ConfigurationService configurationService) {
+ public static Response buildErrorPage(String uri, String base, Status status, String message, ConfigurationService configurationService, TemplatingService templatingService) {
Map<String, Object> data = new HashMap<String, Object>();
-
data.put("uri", uri);
data.put("message", message);
try {
@@ -154,19 +155,10 @@ public class ResourceWebServiceHelper {
} catch (UnsupportedEncodingException uee) {
data.put("encoded_uri", uri);
}
-
- data.put("SERVER_URL", configurationService.getServerUri());
- data.put("BASIC_URL", configurationService.getBaseUri());
- String project = configurationService.getStringConfiguration("kiwi.pages.project", "marmotta");
- data.put("PROJECT", project);
- data.put("LOGO", configurationService.getStringConfiguration("kiwi.pages.project."+project+".logo", project+".png"));
- data.put("CSS", configurationService.getStringConfiguration("kiwi.pages.project."+project+".css", "core/public/style/"+project+".css"));
- data.put("FOOTER", configurationService.getStringConfiguration("kiwi.pages.project."+project+".footer", "(footer not properly configured for project "+project+")"));
- data.put("DEFAULT_STYLE", configurationService.getStringConfiguration("kiwi.pages.style", "marmotta"));
try {
return Response.status(status)
- .entity(TemplatingHelper.processTemplate("404.ftl", data))
+ .entity(templatingService.process(TEMPLATE_404, data))
.build();
} catch (Exception e) {
return Response.status(Response.Status.NOT_FOUND)
[2/2] git commit: fixed 404 template
Posted by wi...@apache.org.
fixed 404 template
Project: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/commit/6059fb83
Tree: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/tree/6059fb83
Diff: http://git-wip-us.apache.org/repos/asf/incubator-marmotta/diff/6059fb83
Branch: refs/heads/master
Commit: 6059fb835bc59c0014bbf4e654c8d7e510b97ae5
Parents: 6c6288f
Author: Sergio Fernández <wi...@apache.org>
Authored: Thu Apr 11 13:20:12 2013 +0200
Committer: Sergio Fernández <wi...@apache.org>
Committed: Thu Apr 11 13:20:12 2013 +0200
----------------------------------------------------------------------
.../src/main/resources/templates/404.ftl | 6 +++---
1 files changed, 3 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/6059fb83/platform/marmotta-core/src/main/resources/templates/404.ftl
----------------------------------------------------------------------
diff --git a/platform/marmotta-core/src/main/resources/templates/404.ftl b/platform/marmotta-core/src/main/resources/templates/404.ftl
index 9775fe1..7303e60 100644
--- a/platform/marmotta-core/src/main/resources/templates/404.ftl
+++ b/platform/marmotta-core/src/main/resources/templates/404.ftl
@@ -26,9 +26,9 @@
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>404 Not Found - Marmotta Linked Data Explorer</title>
<script src="${SERVER_URL}core/public/js/lib/jquery-1.7.2.js" type="text/javascript" ></script>
- <link href="${SERVER_URL}${DEFAULT_STYLE}/style.css" rel="stylesheet" type="text/css" />
- <link href="${SERVER_URL}${DEFAULT_STYLE}/rdfhtml.css" rel="stylesheet" type="text/css" />
- <link href="${SERVER_URL}${DEFAULT_STYLE}/404.css" rel="stylesheet" type="text/css" />
+ <link href="${SERVER_URL}${DEFAULT_STYLE}style.css" rel="stylesheet" type="text/css" />
+ <link href="${SERVER_URL}${DEFAULT_STYLE}rdfhtml.css" rel="stylesheet" type="text/css" />
+ <link href="${SERVER_URL}${DEFAULT_STYLE}404.css" rel="stylesheet" type="text/css" />
</head>
<body>