diff options
author | Adhitya Kamakshidasan | 2016-07-18 22:33:18 +0530 |
---|---|---|
committer | GitHub | 2016-07-18 22:33:18 +0530 |
commit | 6f3bfe880a24caa05399d4c7153037ff88c7c394 (patch) | |
tree | be3a9c1d2458b684c6135fa16484cb77e1373c68 | |
parent | 8c702e995aa4f252b2816b7e905c4c1236673172 (diff) | |
parent | 623f4797aaf29222aab9d9a44d7c2f8d09622d1f (diff) | |
download | xcos-on-web-6f3bfe880a24caa05399d4c7153037ff88c7c394.tar.gz xcos-on-web-6f3bfe880a24caa05399d4c7153037ff88c7c394.tar.bz2 xcos-on-web-6f3bfe880a24caa05399d4c7153037ff88c7c394.zip |
Merge pull request #180 from sarangsingh29/master
Added the servlet code and directions to run.
-rw-r--r-- | WEB-INF/Readme | 9 | ||||
-rwxr-xr-x | WEB-INF/classes/SciExec.class | bin | 0 -> 3308 bytes | |||
-rwxr-xr-x | WEB-INF/classes/SciExec.java | 133 | ||||
-rwxr-xr-x | WEB-INF/classes/SciExec.java~ | 91 | ||||
-rwxr-xr-x | WEB-INF/web.xml | 38 |
5 files changed, 271 insertions, 0 deletions
diff --git a/WEB-INF/Readme b/WEB-INF/Readme new file mode 100644 index 0000000..444d69a --- /dev/null +++ b/WEB-INF/Readme @@ -0,0 +1,9 @@ +/* + Maverick + Directions to run the servlet code. +*/ + +Download apache-tomcat-xxx. +Create a folder 'temp' in the tomcat directory. +Paste the entire 'xcos-on-web' directory inside the '/path/to/tomcat/webapps' directory. +Run the server and access using a web browser.
\ No newline at end of file diff --git a/WEB-INF/classes/SciExec.class b/WEB-INF/classes/SciExec.class Binary files differnew file mode 100755 index 0000000..f87436e --- /dev/null +++ b/WEB-INF/classes/SciExec.class diff --git a/WEB-INF/classes/SciExec.java b/WEB-INF/classes/SciExec.java new file mode 100755 index 0000000..42c185a --- /dev/null +++ b/WEB-INF/classes/SciExec.java @@ -0,0 +1,133 @@ +/* + Servlet code for xcos-on-web + Authors: Maverick and Adhitya + Description: + The following servlet creates a shell command to run Scilab without any GUI/simulation window. + The diagram sent from client is stored in a usr-xxx.xcos file and is then simulated. The result + is a .png file which is then sent back to the client. +*/ +import java.io.*; +import javax.servlet.*; +import java.util.*; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import org.apache.commons.io.IOUtils; +import org.apache.commons.io.FilenameUtils; + + +public class SciExec extends GenericServlet { + + + public void service(ServletRequest request,ServletResponse response) throws ServletException, IOException + { + PrintWriter pw = response.getWriter(); + + + InputStream is = null; + ByteArrayOutputStream baos = null; + List<String> commands = new ArrayList<String>(); + /* + Maverick & Srikant sir. + Creating a shell command to run Scilab without any GUI so that the simulation window doesn't appear. + */ + commands.add("scilab-adv-cli"); + commands.add("-noatomsautoload"); + commands.add("-nogui"); + commands.add("-nb"); + commands.add("-e"); + + String imagePath=""; + String fileNameWithoutExt=""; + + /* + Maverick + Ignore the following comments. + */ + + //---IGNORE--- + /* + commands.add("plot3d();xs2png(gcf(),'img2.png');exit();"); + String data=request.getParameter("name"); + pw.println(data); + commands.add("echo "+data+" > /home/saarang/Softwares/apache-tomcat-8.0.36/webapps/sci/servlet/file.xcos"); + commands.add("driver('PNG');xinit('/home/saarang/apache-tomcat-8.0.36/webapps/worknogui/servlet/ans"+k+".png');loadXcosLibs();importXcosDiagram('/home/saarang/apache-tomcat-8.0.36/file.xcos');xcos_simulate(scs_m,4);mode(2);xend();quit();"); + */ + //--IGNORE till here--- + + + ProcessBuilder pb = new ProcessBuilder(commands); + try + { + InputStream inputStream = request.getInputStream(); + /* + Adhitya + Creating a temporary file in the directory containing tomcat. This file is the .xcos file which will be executed by the Scilab engine. + */ + File xcosFile = File.createTempFile("usr-", ".xcos"); + OutputStream outputStream = new FileOutputStream(xcosFile); + IOUtils.copy(inputStream, outputStream); + outputStream.close(); + + String tempPath = xcosFile.getAbsolutePath(); + + String parentPath = xcosFile.getParent(); + fileNameWithoutExt = FilenameUtils.removeExtension(xcosFile.getName()); + + /* + Maverick + The resultant .png file which is produced as a result of simulation is stored in a different folder inside the respective webapp directory. + The path of the folder is /path/to/webapp/servlet. + + TODO: Modify the following path. + */ + imagePath = "/path/to/webapps/xcos-on-web/servlet"+File.separator + fileNameWithoutExt + ".png"; + + /* + Maverick + Creating the command which is to be executed using terminal. + */ + commands.add("driver('PNG');xinit('"+imagePath+"');loadXcosLibs();importXcosDiagram('"+tempPath+"');xcos_simulate(scs_m,4);mode(2);xend();quit();"); + + /* + Maverick + Reference: www.stackoverflow.com + Starting a process to execute the created command. + */ + Process prs = pb.start(); + is = prs.getInputStream(); + byte[] b = new byte[1024]; + int size = 0; + baos = new ByteArrayOutputStream(); + while((size = is.read(b)) != -1){ + baos.write(b, 0, size); + } + System.out.println(new String(baos.toByteArray())); + + + } + catch (IOException e) + { + e.printStackTrace(); + } + catch(Exception e) + { + pw.println("Exception"); + } + finally + { + try { + if(is != null) is.close(); + if(baos != null) baos.close(); + } catch (Exception ex){} + } + /* + Maverick + Accessing the .png file and sending it to the client. + */ + pw.println("servlet/"+fileNameWithoutExt+".png"); + pw.close(); + } +} diff --git a/WEB-INF/classes/SciExec.java~ b/WEB-INF/classes/SciExec.java~ new file mode 100755 index 0000000..c4287aa --- /dev/null +++ b/WEB-INF/classes/SciExec.java~ @@ -0,0 +1,91 @@ +import java.io.*; +import javax.servlet.*; +import java.util.*; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; +import org.apache.commons.io.IOUtils; +import org.apache.commons.io.FilenameUtils; + + +public class SciExec extends GenericServlet { + + + public void service(ServletRequest request,ServletResponse response) throws ServletException, IOException + { + PrintWriter pw = response.getWriter(); + + + + + InputStream is = null; + ByteArrayOutputStream baos = null; + List<String> commands = new ArrayList<String>(); + commands.add("scilab-adv-cli"); + commands.add("-noatomsautoload"); + commands.add("-nogui"); + commands.add("-nb"); + commands.add("-e"); + + String imagePath=""; + String fileNameWithoutExt=""; + //commands.add("plot3d();xs2png(gcf(),'img2.png');exit();"); + /*String data=request.getParameter("name"); + pw.println(data); + commands.add("echo "+data+" > /home/saarang/Softwares/apache-tomcat-8.0.36/webapps/sci/servlet/file.xcos");*/ + //commands.add("driver('PNG');xinit('/home/saarang/apache-tomcat-8.0.36/webapps/worknogui/servlet/ans"+k+".png');loadXcosLibs();importXcosDiagram('/home/saarang/apache-tomcat-8.0.36/file.xcos');xcos_simulate(scs_m,4);mode(2);xend();quit();"); + ProcessBuilder pb = new ProcessBuilder(commands); + try + { + InputStream inputStream = request.getInputStream(); + + File xcosFile = File.createTempFile("usr-", ".xcos"); + OutputStream outputStream = new FileOutputStream(xcosFile); + IOUtils.copy(inputStream, outputStream); + outputStream.close(); + + String tempPath = xcosFile.getAbsolutePath(); + + String parentPath = xcosFile.getParent(); + fileNameWithoutExt = FilenameUtils.removeExtension(xcosFile.getName()); + + imagePath = "/home/saarang/apache-tomcat-8.0.36/webapps/xcos-on-web/servlet"+File.separator + fileNameWithoutExt + ".png"; + + commands.add("driver('PNG');xinit('"+imagePath+"');loadXcosLibs();importXcosDiagram('"+tempPath+"');xcos_simulate(scs_m,4);mode(2);xend();quit();"); + + Process prs = pb.start(); + is = prs.getInputStream(); + byte[] b = new byte[1024]; + int size = 0; + baos = new ByteArrayOutputStream(); + while((size = is.read(b)) != -1){ + baos.write(b, 0, size); + } + System.out.println(new String(baos.toByteArray())); + + + } + catch (IOException e) + { + e.printStackTrace(); + } + catch(Exception e) + { + /*System.out.println(e.toString()+"Some Exception");*/ + pw.println("Exception"); + } + finally + { + try { + if(is != null) is.close(); + if(baos != null) baos.close(); + } catch (Exception ex){} + } + pw.println("servlet/"+fileNameWithoutExt+".png"); + //pw.println(request.getParameter("name")); + pw.close(); + } + +} diff --git a/WEB-INF/web.xml b/WEB-INF/web.xml new file mode 100755 index 0000000..f74ebf3 --- /dev/null +++ b/WEB-INF/web.xml @@ -0,0 +1,38 @@ +<?xml version="1.0" encoding="ISO-8859-1"?> +<!-- + 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. +--> + +<web-app xmlns="http://java.sun.com/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" + version="2.5"> + + <servlet> + <servlet-name>SciExec</servlet-name> + <servlet-class>SciExec</servlet-class> + </servlet> + <servlet-mapping> + <servlet-name>SciExec</servlet-name> + <url-pattern>/servlet/SciExec</url-pattern> + </servlet-mapping> + + + <display-name>Welcome to Tomcat</display-name> + <description> + Welcome to Tomcat + </description> +</web-app> |