Minor Update

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/tabular-data-expression-widget@91545 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Giancarlo Panichi 2014-02-13 15:34:24 +00:00
parent 5f0c12e49e
commit ffe640fd49
3 changed files with 154 additions and 45 deletions

View File

@ -3,11 +3,9 @@
*/ */
package org.gcube.portlets.user.td.expressionwidget.server; package org.gcube.portlets.user.td.expressionwidget.server;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -18,81 +16,89 @@ import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import org.gcube.data.analysis.tabulardata.expression.Expression; import org.gcube.data.analysis.tabulardata.expression.Expression;
import org.gcube.data.analysis.tabulardata.expression.MalformedExpressionException;
import org.gcube.data.analysis.tabulardata.expression.composite.comparable.GreaterThan; import org.gcube.data.analysis.tabulardata.expression.composite.comparable.GreaterThan;
import org.gcube.data.analysis.tabulardata.expression.leaf.ColumnReferencePlaceholder; import org.gcube.data.analysis.tabulardata.expression.leaf.ColumnReferencePlaceholder;
import org.gcube.data.analysis.tabulardata.expression.leaf.ConstantList; import org.gcube.data.analysis.tabulardata.expression.leaf.ConstantList;
import org.gcube.data.analysis.tabulardata.model.datatype.DataType;
import org.gcube.data.analysis.tabulardata.model.datatype.IntegerType; import org.gcube.data.analysis.tabulardata.model.datatype.IntegerType;
import org.gcube.data.analysis.tabulardata.model.datatype.value.TDInteger; import org.gcube.data.analysis.tabulardata.model.datatype.value.TDInteger;
import org.gcube.data.analysis.tabulardata.model.datatype.value.TDTypeValue; import org.gcube.data.analysis.tabulardata.model.datatype.value.TDTypeValue;
import org.gcube.portlets.user.td.expressionwidget.shared.model.C_Expression; import org.gcube.portlets.user.td.expressionwidget.shared.model.C_Expression;
import org.gcube.portlets.user.td.expressionwidget.shared.model.leaf.TD_Value;
import org.gcube.portlets.user.td.gwtservice.server.file.FileUploadSession;
import org.gcube.portlets.user.td.gwtservice.shared.csv.CSVFileUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* *
* @author "Giancarlo Panichi" * @author "Giancarlo Panichi" <a
* <a href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a> * href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a>
* *
*/ */
public class CExpressionMapServlet extends HttpServlet { public class CExpressionMapServlet extends HttpServlet {
private static final long serialVersionUID = 1352918477757397708L; private static final long serialVersionUID = 1352918477757397708L;
protected static Logger logger = LoggerFactory.getLogger(CExpressionMapServlet.class); protected static Logger logger = LoggerFactory
.getLogger(CExpressionMapServlet.class);
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { throws ServletException, IOException {
handleRequest(req, resp); handleRequest(req, resp);
} }
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
handleRequest(req, resp); handleRequest(req, resp);
} }
protected void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
logger.info("CExpressionMapServlet"); logger.info("CExpressionMapServlet");
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
HttpSession session = request.getSession(); HttpSession session = request.getSession();
if (session == null) { if (session == null) {
logger.error("Error getting the upload session, no session valid found: "+session); logger.error("Error getting the upload session, no session valid found: "
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "ERROR-Error getting the user session, no session found"+session); + session);
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"ERROR-Error getting the user session, no session found"
+ session);
return; return;
} }
logger.info("CExpressionMapServlet import session id: "+session.getId()); logger.info("CExpressionMapServlet import session id: "
+ session.getId());
ObjectInputStream in = new ObjectInputStream(request.getInputStream());
C_Expression exp=null;
try {
exp = (C_Expression) in.readObject();
logger.debug("CExpressionMapServlet Exp: "+exp);
} catch (ClassNotFoundException e) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST,
"ERROR-Error retreiving expression in request: "+e.getLocalizedMessage());
//response.setContentType("application/json; charset=utf-8"); }
response.setStatus(HttpServletResponse.SC_OK);
C_Expression exp=(C_Expression)request.getAttribute("C_Expression");
Expression expression = parse(exp); Expression expression = parse(exp);
logger.trace("Expression: " + expression.toString()); logger.trace("Expression: " + expression.toString());
ObjectOutputStream oos = new ObjectOutputStream(response.getOutputStream()); response.setContentType("Content-Type: application/java-object");
response.setStatus(HttpServletResponse.SC_OK);
ObjectOutputStream oos = new ObjectOutputStream(
response.getOutputStream());
oos.writeObject(expression); oos.writeObject(expression);
oos.flush(); oos.flush();
oos.close(); oos.close();
logger.trace("Response in " + (System.currentTimeMillis() - startTime)); logger.trace("Response in " + (System.currentTimeMillis() - startTime));
} }
protected Expression parse(C_Expression exp) { protected Expression parse(C_Expression exp) {
List<TDTypeValue> args = new ArrayList<TDTypeValue>(); List<TDTypeValue> args = new ArrayList<TDTypeValue>();
TDInteger value = new TDInteger(1); TDInteger value = new TDInteger(1);
args.add(value); args.add(value);
Expression e=new GreaterThan(new ColumnReferencePlaceholder(new IntegerType(), "1"), Expression e = new GreaterThan(new ColumnReferencePlaceholder(
new ConstantList(args)); new IntegerType(), "1"), new ConstantList(args));
return e; return e;
} }

View File

@ -6,6 +6,11 @@
<web-app> <web-app>
<!-- Servlets --> <!-- Servlets -->
<servlet>
<servlet-name>CExpressionMapServlet</servlet-name>
<servlet-class>org.gcube.portlets.user.td.expressionwidget.server.CExpressionMapServlet</servlet-class>
</servlet>
<servlet> <servlet>
<servlet-name>TDGWTService</servlet-name> <servlet-name>TDGWTService</servlet-name>
<servlet-class>org.gcube.portlets.user.td.gwtservice.server.TDGWTServiceImpl</servlet-class> <servlet-class>org.gcube.portlets.user.td.gwtservice.server.TDGWTServiceImpl</servlet-class>
@ -17,6 +22,10 @@
</servlet> </servlet>
<!-- Servlets Mapping --> <!-- Servlets Mapping -->
<servlet-mapping>
<servlet-name>CExpressionMapServlet</servlet-name>
<url-pattern>ExpressionWidget/CExpressionMap</url-pattern>
</servlet-mapping>
<servlet-mapping> <servlet-mapping>
<servlet-name>TDGWTService</servlet-name> <servlet-name>TDGWTService</servlet-name>
<url-pattern>ExpressionWidget/TDGWTService</url-pattern> <url-pattern>ExpressionWidget/TDGWTService</url-pattern>

View File

@ -0,0 +1,94 @@
package org.gcube.portlets.user.td.expressionwidget.client;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import org.gcube.data.analysis.tabulardata.expression.Expression;
import org.gcube.portlets.user.td.expressionwidget.shared.model.C_Expression;
import org.junit.Test;
public class TestCExpressionServlet {
@Test
public void test() {
HttpURLConnection cs = null;
String path = "ExpressionWidget/CExpressionMap";
URL url = null;
try {
url = new URL("http://127.0.0.1:8888/" + path);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
C_Expression exp = new C_Expression();
// Save the object's instance
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
ObjectOutputStream out;
try {
out = new ObjectOutputStream(buffer);
out.writeObject(exp);
} catch (IOException e) {
System.out.println("IOException :" + e.getLocalizedMessage());
return;
}
// Prepare the header
byte[] serobj = buffer.toByteArray();
try {
cs = (HttpURLConnection) url.openConnection();
cs.setConnectTimeout(0);
cs.setReadTimeout(0);
cs.setRequestMethod("POST");
cs.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
cs.setDoOutput(true);
cs.setDoInput(true);
cs.setUseCaches(false);
cs.setAllowUserInteraction(false);
cs.setRequestProperty("Content-Type", "application/java-object");
cs.setRequestProperty("Content-Language", "en-US");
cs.setRequestProperty("Content-Length",
String.valueOf(serobj.length));
OutputStream sout = cs.getOutputStream();
sout.write(serobj);
int HTTPcodicerisposta = cs.getResponseCode();
System.out.println("Codice Risposta: " + HTTPcodicerisposta);
if (HTTPcodicerisposta == 200) {
ObjectInputStream oin = new ObjectInputStream(
cs.getInputStream());
Expression serverExpression = null;
try {
serverExpression = (Expression) oin.readObject();
} catch (ClassNotFoundException e) {
System.out.println("Read Object error :"
+ e.getLocalizedMessage());
e.printStackTrace();
}
System.out.println(serverExpression);
} else {
System.out.println("Codice Risposta: " + HTTPcodicerisposta);
}
} catch (IOException e1) {
System.out.println("Error: "+e1.getLocalizedMessage());
e1.printStackTrace();
} finally {
cs.disconnect();
}
}
}