From: <ku...@us...> - 2011-06-09 16:12:25
|
Revision: 2858 http://dl-learner.svn.sourceforge.net/dl-learner/?rev=2858&view=rev Author: kurzum Date: 2011-06-09 16:12:18 +0000 (Thu, 09 Jun 2011) Log Message: ----------- added the webservice, the code is untested as i still can not compile DL-Learner at the moment, go to interfaces and start with mvn jetty:run Modified Paths: -------------- trunk/interfaces/pom.xml Added Paths: ----------- trunk/interfaces/src/main/java/org/dllearner/server/Rest.java trunk/interfaces/src/main/webapp/ trunk/interfaces/src/main/webapp/WEB-INF/ trunk/interfaces/src/main/webapp/WEB-INF/web.xml Modified: trunk/interfaces/pom.xml =================================================================== --- trunk/interfaces/pom.xml 2011-06-08 18:10:11 UTC (rev 2857) +++ trunk/interfaces/pom.xml 2011-06-09 16:12:18 UTC (rev 2858) @@ -1,88 +1,112 @@ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> - <groupId>org.dllearner</groupId> - <artifactId>interfaces</artifactId> - <packaging>jar</packaging> - <name>Interfaces: GUI, CLI, Web Service </name> - <url>http://aksw.org/Projects/DLLearner</url> + <groupId>org.dllearner</groupId> + <artifactId>interfaces</artifactId> + <packaging>jar</packaging> + <name>Interfaces: GUI, CLI, Web Service</name> + <url>http://aksw.org/Projects/DLLearner</url> - <parent> - <groupId>org.dllearner</groupId> - <artifactId>dllearner-parent</artifactId> - <version>1.0-SNAPSHOT</version> - </parent> + <parent> + <groupId>org.dllearner</groupId> + <artifactId>dllearner-parent</artifactId> + <version>1.0-SNAPSHOT</version> + </parent> - <build> - <plugins> - <!--Surefire - for JUnits --> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-surefire-plugin</artifactId> - <configuration> - <excludes> - <exclude>org/dllearner/test/junit/OWLlinkTest.java</exclude> - </excludes> - </configuration> - </plugin> - <plugin> - <artifactId>jdeb</artifactId> - <groupId>org.vafer</groupId> - <version>0.8</version> - <executions> - <execution> - <phase>package</phase> - <goals> - <goal>jdeb</goal> - </goals> - <configuration> - <dataSet> - <data> - <src>${project.build.directory}/${project.build.finalName}.jar</src> - <type>file</type> - <mapper> - <type>perm</type> - <prefix>/usr/share/dllearner</prefix> - </mapper> - </data> - </dataSet> - </configuration> - </execution> - </executions> - </plugin> - </plugins> - </build> + <build> + <finalName>dl-learner</finalName> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>tomcat-maven-plugin</artifactId> + </plugin> + <plugin> + <groupId>org.mortbay.jetty</groupId> + <artifactId>maven-jetty-plugin</artifactId> + </plugin> + <!--Surefire - for JUnits --> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <excludes> + <exclude>org/dllearner/test/junit/OWLlinkTest.java</exclude> + </excludes> + </configuration> + </plugin> + <plugin> + <artifactId>jdeb</artifactId> + <groupId>org.vafer</groupId> + <version>0.8</version> + <executions> + <execution> + <phase>package</phase> + <goals> + <goal>jdeb</goal> + </goals> + <configuration> + <dataSet> + <data> + <src>${project.build.directory}/${project.build.finalName}.jar</src> + <type>file</type> + <mapper> + <type>perm</type> + <prefix>/usr/share/dllearner</prefix> + </mapper> + </data> + </dataSet> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> - <dependencies> - <dependency> - <groupId>org.dllearner</groupId> - <artifactId>components-core</artifactId> - </dependency> + <dependencies> + <dependency> + <groupId>org.dllearner</groupId> + <artifactId>components-core</artifactId> + </dependency> - <!--Added the dependency of the core tests so that they will be accessible - from the tests in this component --> - <dependency> - <groupId>org.dllearner</groupId> - <artifactId>components-core</artifactId> - <classifier>tests</classifier> - <scope>test</scope> - </dependency> + <!--Added the dependency of the core tests so that they will be accessible + from the tests in this component --> + <dependency> + <groupId>org.dllearner</groupId> + <artifactId>components-core</artifactId> + <classifier>tests</classifier> + <scope>test</scope> + </dependency> - <dependency> - <groupId>org.dllearner</groupId> - <artifactId>components-ext</artifactId> - </dependency> - <dependency> - <groupId>com.jamonapi</groupId> - <artifactId>jamon</artifactId> - </dependency> + <dependency> + <groupId>org.dllearner</groupId> + <artifactId>components-ext</artifactId> + </dependency> + <dependency> + <groupId>com.jamonapi</groupId> + <artifactId>jamon</artifactId> + </dependency> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <scope>test</scope> - </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + </dependency> - </dependencies> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>servlet-api</artifactId> + </dependency> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>jsp-api</artifactId> + </dependency> + <dependency> + <groupId>javax.servlet</groupId> + <artifactId>jstl</artifactId> + </dependency> + + </dependencies> + + </project> Added: trunk/interfaces/src/main/java/org/dllearner/server/Rest.java =================================================================== --- trunk/interfaces/src/main/java/org/dllearner/server/Rest.java (rev 0) +++ trunk/interfaces/src/main/java/org/dllearner/server/Rest.java 2011-06-09 16:12:18 UTC (rev 2858) @@ -0,0 +1,140 @@ +package org.dllearner.server; + +import com.jamonapi.Monitor; +import com.jamonapi.MonitorFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.net.URLEncoder; +import java.security.InvalidParameterException; + + +public abstract class Rest extends HttpServlet { + private static Logger log = LoggerFactory.getLogger(Rest.class); + + @Override + protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { + handle(httpServletRequest, httpServletResponse); + } + + @Override + protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { + handle(httpServletRequest, httpServletResponse); + } + + + /** + * * + * + * @param httpServletRequest + * @param httpServletResponse + * @throws ServletException + * @throws java.io.IOException + */ + private void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { + Monitor mon = MonitorFactory.getTimeMonitor("NIFParameters.getInstance").start(); + String result = ""; + try { + //check parameters + String conf = ""; + if (isSet(httpServletRequest, "conf")) { + conf = httpServletRequest.getParameter("conf"); + } else { + throw new InvalidParameterException("No parameter 'conf' found. " + getDocumentation(httpServletRequest.getRequestURL().toString())); + } + + //output default is json + String output = "json"; + if (isSet(httpServletRequest, "output")) { + output = httpServletRequest.getParameter("output"); + //use the function one of + if (!oneOf(output, "json", "xml")) { + throw new InvalidParameterException("Wrong parameter value for \"output\", must be one of ( json, xml ) " + getDocumentation(httpServletRequest.getRequestURL().toString())); + } + } + + /** + * Do the magic here + */ + result = conf; + + PrintWriter pw = httpServletResponse.getWriter(); + log.debug("Request handled: " + logMonitor(mon.stop())); + pw.print(result); + pw.close(); + + } catch (IllegalArgumentException e) { + String msg = e.getMessage() + printParameterMap(httpServletRequest); + log.error(msg); + httpServletResponse.setContentType("text/plain"); + PrintWriter out = httpServletResponse.getWriter(); + out.println(msg); + out.close(); + + } catch (Exception e) { + String msg = "An error occured: " + e.getMessage() + printParameterMap(httpServletRequest); + log.error(msg, e); + httpServletResponse.setContentType("text/plain"); + PrintWriter out = httpServletResponse.getWriter(); + out.println(msg); + out.close(); + + } + + } + + + /** + * Examples are from NIF + * + * @param serviceUrl + * @return + */ + public static String getDocumentation(String serviceUrl) { + String doc = ""; + try { + doc = "\nExample1: \n " + serviceUrl + "?input=" + URLEncoder.encode("That's a lot of nuts! That'll be four bucks, baby! You want fries with that? ", "UTF-8") + "&type=text"; + doc += "\nExample2: \n " + serviceUrl + "?input=" + URLEncoder.encode("That's a lot of nuts! That's a lot of nuts! ", "UTF-8") + "&type=text"; + } catch (Exception e) { + log.error("", e); + } + return doc; + } + + public static boolean isSet(HttpServletRequest httpServletRequest, String name) { + log.trace("isSet(" + name + ")"); + log.trace(httpServletRequest.getParameterValues(name) + ""); + return httpServletRequest.getParameterValues(name) != null && httpServletRequest.getParameterValues(name).length == 1 && httpServletRequest.getParameter(name).length() > 0; + } + + public static boolean oneOf(String value, String... possibleValues) { + for (String s : possibleValues) { + if (s.equals(value)) { + return true; + } + } + return false; + } + + protected static String logMonitor(Monitor m) { + return "needed: " + m.getLastValue() + " ms. (" + m.getTotal() + " total)"; + } + + public static String printParameterMap(HttpServletRequest httpServletRequest) { + StringBuffer buf = new StringBuffer(); + for (Object key : httpServletRequest.getParameterMap().keySet()) { + buf.append("\nParameter: " + key + " Values: "); + for (String s : httpServletRequest.getParameterValues((String) key)) { + buf.append(((s.length() > 200) ? s.substring(0, 200) + "..." : s) + " "); + } + } + return buf.toString(); + } + +} Added: trunk/interfaces/src/main/webapp/WEB-INF/web.xml =================================================================== --- trunk/interfaces/src/main/webapp/WEB-INF/web.xml (rev 0) +++ trunk/interfaces/src/main/webapp/WEB-INF/web.xml 2011-06-09 16:12:18 UTC (rev 2858) @@ -0,0 +1,19 @@ +<!DOCTYPE web-app PUBLIC + "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" + "http://java.sun.com/dtd/web-app_2_3.dtd" > + +<web-app> + <display-name>DL-Learner</display-name> + + + <servlet> + <servlet-name>rest</servlet-name> + <servlet-class>org.dllearner.server.Rest</servlet-class> + </servlet> + + <servlet-mapping> + <servlet-name>rest</servlet-name> + <url-pattern>/rest</url-pattern> + </servlet-mapping> + +</web-app> This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |