Commit bf2db246 authored by Kjetil Thuen's avatar Kjetil Thuen

Removed Java code

parent f301f371
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.nesstar</groupId>
<artifactId>NesstarMobile</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>NesstarMobile Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
</properties>
<repositories>
<repository>
<id>Nesstar</id>
<name>Nesstar Maven Repository</name>
<url>http://nesstar-dev.nsd.uib.no/nexus/content/groups/public/</url>
</repository>
<repository>
<id>sourceforge</id>
<url>http://oss.sonatype.org/content/groups/sourceforge/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>com.nesstar</groupId>
<artifactId>nesstar-api</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- <<version>0.5</version> -->
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.6.2</version>
<type>pom</type>
</dependency>
</dependencies>
<build>
<finalName>NesstarMobile</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<configuration>
<server>LocalTomcat</server>
<path>/NesstarMobile</path>
</configuration>
</plugin>
<!-- Run the application using "mvn jetty" -->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<scanIntervalSeconds>7</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>9998</stopPort>
<scanTargetPatterns>
<!-- Redeploy whenever any of the following files change -->
<scanTargetPattern>
<directory>src/main/webapp</directory>
<includes>
<include>**/*.html</include>
<include>**/*.js</include>
<include>**/*.css</include>
<include>**/*.less</include>
</includes>
</scanTargetPattern>
<scanTargetPattern>
<directory>src/main/java</directory>
<includes>
<include>**/*.java</include>
</includes>
</scanTargetPattern>
</scanTargetPatterns>
</configuration>
<executions>
<!-- Launch maven when running integration tests -->
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
package com.nesstar;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.Date;
import java.util.Hashtable;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import com.nesstar.api.NotAuthorizedException;
import com.nesstar.server.SingletonServer;
/**
* The servlet of the project.
*
*/
public class NesstarMobile extends HttpServlet {
private static final long serialVersionUID = 4328713;
private static final Logger logger = Logger.getLogger(NesstarMobile.class);
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (null == request.getSession().getAttribute(StaticVariables.DAO)) {
request.getSession().setAttribute(StaticVariables.DAO, new TabulateAnalysisDAO(request.getSession().getId()));
}
// set charset for the page
response.setContentType("application/json;charset=utf-8");
// set session for the request
SingletonServer.setSession(request.getSession().getId());
URLParser parser = new URLParser(request.getPathInfo());
String filename, json = "";
if ("POST".equals(request.getMethod())) {
if (parser.isLogin()) {
StringBuffer jsonInput = new StringBuffer();
String line = null;
BufferedReader reader = request.getReader();
while((line = reader.readLine()) != null) {
jsonInput.append(line);
}
JSONObject jsonObjectInput = JSONObject.fromObject(jsonInput.toString());
Map<String, Object> jsonResponseObject = new Hashtable<String, Object>();
if ("admin".equals(jsonObjectInput.get("username")) && "admin".equals(jsonObjectInput.get("password"))) {
Map<String, String> login = new Hashtable<String, String>();
login.put("username", jsonObjectInput.getString("username"));
jsonResponseObject.put("login", login);
json = JSONObject.fromObject(jsonResponseObject).toString();
} else {
Map<String, String> error = new Hashtable<String, String>();
error.put("type", "401");
error.put("message", "Login failed");
jsonResponseObject.put("error", error);
json = JSONObject.fromObject(jsonResponseObject).toString();
response.setStatus(401);
}
} else {
doPost(request, response);
return;
}
}
if ("GET".equals(request.getMethod())) {
logger.info(request.getPathInfo());
if (parser.isStudyListRequest()) {
filename = "studylist.json";
} else if (parser.isStudyRequest()) {
filename = "study.json";
} else if (parser.isStudyMetadataRequest()) {
filename = "studymetadata.json";
} else if (parser.isVariableListRequest() || parser.isVariableGroupVariablesRequest()) {
filename = "variablesingroups.json";
} else if (parser.isVariableGroupListRequest() || parser.isVariableGroupGroupsRequest()) {
filename = "variablegroups.json";
} else if (parser.isVariableRequest()) {
filename = "variable.json";
} else {
//still use old stuff if the above doesn't work
doGet(request, response);
logger.info("do get");
return;
}
logger.info(filename);
json = getJSONfile(filename);
}
write(json, response);
}
private String getJSONfile(String filename) {
File file = new File("./src/main/resources/" + filename);
StringBuffer buffer = new StringBuffer();
try {
FileInputStream stream = new FileInputStream(file);
int character;
while ((character = stream.read()) != -1) {
buffer.append((char) character);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return buffer.toString();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
String operation = request.getParameter("operation");
if (operation == null) {
operation = "getStudyList";
}
TabulateAnalysisDAO dao = (TabulateAnalysisDAO) request.getSession().getAttribute(StaticVariables.DAO);
logger.info("Access:" + request.getRemoteAddr() + ", operation: " + operation + ", time: " + new Date());
try {
String content = "";
// if (checkSession(dao, request, response)) {
if ("islogin".equals(operation)) {
content = dao.checkLogin();
} else if ("getStudyList".equals(operation)) {
content = dao.getStudyList();
} else if ("getVariables".equals(operation)) {
content = dao.getVariableGroups();
} else if ("getVariableMetadata".equals(operation)) {
String variableName = request.getParameter("variablename");
content = dao.getInformationGivenVariable(variableName);
} else if ("getStudyMetadata".equals(operation)) {
content = dao.getStudyMetadata();
} else if ("getPartMetadata".equals(operation)) {
String selectedStudy = (String) request.getParameter("study");
content = dao.getPartOfStudyMetadata(selectedStudy);
}
// } else {
// noAuthorizedError();
// content = redirectToLogin();
// }
write(content, response);
} catch (NotAuthorizedException e) {
logger.warn(e);
noAuthorizedError();
} catch (IOException e) {
logger.error(e);
generalError("An error occurred when communicating with the Nesstar Server");
}
}
@Override
public void destroy() {
super.destroy();
logger.info("Destroy the servlet!");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String operation = request.getParameter("operation");
if (operation == null) {
String error = noOperationError();
write(error, response);
return;
}
String selectedStudy = (String) request.getParameter("study");
TabulateAnalysisDAO dao = (TabulateAnalysisDAO) request.getSession().getAttribute(StaticVariables.DAO);
logger.info("Access:" + request.getRemoteAddr() + ", operation: " + operation + ", time: " + new Date());
String content = "";
if ("login".equals(operation)) {
content = handleLogin(request, response, dao);
} else {
try {
if ("setStudy".equals(operation)) {
content = dao.setStudy(selectedStudy);
} else if ("tabulation".equals(operation)) {
String data = request.getParameter("data");
content = dao.calculateTabulate(data);
} else if ("correlation".equals(operation)) {
String data = request.getParameter("data");
content = dao.calculateCorrelation(data);
} else if ("regression".equals(operation)) {
String data = request.getParameter("data");
content = dao.calculateRegression(data);
} else if ("logout".equals(operation)) {
request.getSession().invalidate();
dao.logout();
}
} catch (NotAuthorizedException e) {
logger.warn(e);
write(noAuthorizedError(), response);
} catch (IOException e) {
logger.error(e);
write(generalError("An error occurred when communicating with the Nesstar Server"), response);
}
}
write(content, response);
}
private String handleLogin(HttpServletRequest request, HttpServletResponse response, TabulateAnalysisDAO dao) throws IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
JSONObject json = new JSONObject();
if (dao.login(username, password)) {
json.element("isLogin", true);
} else {
json.element("isLogin", false);
}
return json.toString();
}
/**
* No authorize error.
*
* @param response
* The http servlet response
*/
private String noAuthorizedError() {
return createErrorJson("notAuthorizedError", "You are not authorized to perform this operation").toString();
}
private String generalError(String message) {
return createErrorJson("general", message).toString();
}
private String noOperationError() {
return createErrorJson("operation", "No operation parameter supplied.").toString();
}
private JSONObject createErrorJson(String type, String reason) {
JSONObject json = new JSONObject();
JSONObject errorJson = new JSONObject();
errorJson.element("type", type);
errorJson.element("message", reason);
json.element("error", errorJson);
return json;
}
private void write(InputStream file, HttpServletResponse response) {
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json");
try {
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, "UTF-8"));
Reader reader = new BufferedReader(new InputStreamReader(file, "UTF-8"));
int character;
while((character = reader.read()) > -1) {
writer.write(character);
writer.flush();
}
response.setContentLength(out.size());
response.setHeader("Content-Encoding", "UTF-8");
out.writeTo(response.getOutputStream());
writer.flush();
writer.close();
reader.close();
} catch (IOException e) {
logger.error("Unable to write", e);
}
}
private void write(String content, HttpServletResponse response) {
try {
response.getWriter().print(content);
} catch (IOException e) {
logger.error("Response Written Error!");
}
}
}
package com.nesstar;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import org.apache.log4j.Logger;
public class ServerHttpSessionListener implements HttpSessionListener {
private static final Logger logger = Logger.getLogger(ServerHttpSessionListener.class);
@Override
public void sessionCreated(HttpSessionEvent se) {
logger.info("Session created: " + se.getSession().getId());
}
@Override
public void sessionDestroyed(HttpSessionEvent event) {
HttpSession session = event.getSession();
// remove the cache of this session
if (null != session.getAttribute(StaticVariables.DAO)) {
((TabulateAnalysisDAO) session.getAttribute(StaticVariables.DAO)).endCache();
}
logger.info("Clear the cache of session: " + session.getId());
logger.info("Session destroyed: " + session.getId());
}
}
package com.nesstar;
import com.nesstar.api.analysis.Statistic;
/**
* Some static variables.
*
* @author Feng Xue
*
*/
public final class StaticVariables {
/**
* The attribute text of td.
*/
public static final String NESSTAR_TEXT_TEXT = "text";
/**
* The text of DAO.
*/
public static final String DAO = "DAO";
public static final String NESSTAR_TEXT_DATA = "data";
public static final String TOTAL_VALUE = "Total";
/**
* The text of variable name with only one (measure) variable.
*/
public static final String THE_OTHER_VARIABLE_NAME_WITH_ONE_BREAK_VARIABLE = "The_Items_With_Only_Break_Variable";
public static final String THE_OTHER_VARIABLE_NAME_WITH_ONE_MEASURE_VARIABLE = "The_Items_With_Only_Measure_Variable";
/**
* The common value of span in the table.
*/
public static final int TABLE_COMMON_SPAN = 1;
public static final String NESSTAR_COLSPAN = "colspan";
public static final String NESSTAR_ROWSPAN = "rowspan";
public static final int HUNDRE_PERCENT = 100;
public static final String FLOAT_ZERO = "0.0";
public static final String INT_ZERO = "0";
/**
* The number of item will be displayed in the table, it is Correlation, Significance, Count.
*/
public static final int ITEM_OF_CORRELATION = 3;
public static final String STUDYS = "studies";
public static final String VARIABLES = "variables";
public static final String SESSIONDATA_METADATA = "Metadata";
public static final String VARIABLE_NAME_TEXT = "name";
public static final String CATEGORY_NAME_TEXT = "name";
public static final String CATEGORY_INDEX_TEXT = "categoryIndex";
public static final String CATEGORY_LIST_TEXT = "categoryList";
public static final String VARIABLE_NAME_CATEGORY_BELONGED_TEXT = "variableBelonged";
public static final String NESTED_ARRAYS_OF_DATA_TEXT = "nestedArraysOfData";
public static final String VARIABLE_LIST_TEXT = "variableList";
/**
* The node of the xml file.
*/
public static final String SMD_ROOT_NODE = ".";
public static final String SMD_BACKSLASH = "/";
public static final String SMD_ATTR_AFFILIATION = SMD_BACKSLASH + "@affiliation";
public static final String SMD_ATTR_ABBREVIATION = SMD_BACKSLASH + "@abbr";
public static final String SMD_ATTR_ROLE = SMD_BACKSLASH + "@role";
public static final String SMD_ATTR_GRANT = SMD_BACKSLASH + "@grant";
public static final String SMD_ATTR_URI = SMD_BACKSLASH + "@URI";
public static final String SMD_ATTR_DATE = SMD_BACKSLASH + "@date";
public static final String SMD_ATTR_END = SMD_BACKSLASH + "@end";
public static final String SMD_ATTR_CYCLE = SMD_BACKSLASH + "@cycle";
public static final String SMD_ATTR_EVENT = SMD_BACKSLASH + "@event";
public static final String SMD_STUDY_DESC = SMD_BACKSLASH + "stdyDscr";
public static final String SMD_CITATION = SMD_BACKSLASH + "citation";
public static final String SMD_TITLE_STATEMENT = SMD_BACKSLASH + "titlStmt";
public static final String SMD_TITLE = SMD_BACKSLASH + "titl";
public static final String SMD_SUBTITLE = SMD_BACKSLASH + "subTitl";
public static final String SMD_ID = SMD_BACKSLASH + "IDNo";
public static final String SMD_RSP_STATEMENT = SMD_BACKSLASH + "rspStmt";
public static final String SMD_AUTHORING_ENTITY = SMD_BACKSLASH + "AuthEnty";
public static final String SMD_PRODUCER_STATEMENT = SMD_BACKSLASH + "prodStmt";
public static final String SMD_COPYRIGHT = SMD_BACKSLASH + "copyright";
public static final String SMD_PRODUCER = SMD_BACKSLASH + "producer";
public static final String SMD_PRODUCER_DATE = SMD_BACKSLASH + "prodDate";
public static final String SMD_PRODUCER_PLACE = SMD_BACKSLASH + "prodPlac";
public static final