From 716b741aa02acce0f1d475afdcd62cd9277cb460 Mon Sep 17 00:00:00 2001 From: Happykiller Date: Tue, 9 Sep 2014 18:09:00 +0200 Subject: [PATCH] inputs, localstorage, connection bonitabpm --- testAPI/pom.xml | 12 +++ .../com/bonitaSoft/business/SystemOpImpl.java | 62 ++++++++++++ .../com/bonitaSoft/business/TestImpl.java | 18 ++++ .../java/com/bonitaSoft/testAPI/SystemOp.java | 94 +++++++++++++++++++ .../java/com/bonitaSoft/testAPI/Test.java | 75 +++++++++++---- .../com/bonitaSoft/tools/LocalStorage.java | 15 +++ .../java/com/bonitaSoft/tools/LoginInfo.java | 73 ++++++++++++++ .../bonitaSoft/tools/ServletContextClass.java | 24 +++++ .../main/java/com/bonitaSoft/tools/Tools.java | 93 +++++++++++++++++- .../com/bonotaSoft/testAPI/MyResource.java | 32 ------- .../java/com/bonotaSoft/testAPI/test.java | 44 --------- testAPI/src/main/resources/config.properties | 3 + testAPI/src/main/webapp/WEB-INF/web.xml | 3 + 13 files changed, 455 insertions(+), 93 deletions(-) create mode 100644 testAPI/src/main/java/com/bonitaSoft/business/SystemOpImpl.java create mode 100644 testAPI/src/main/java/com/bonitaSoft/business/TestImpl.java create mode 100644 testAPI/src/main/java/com/bonitaSoft/testAPI/SystemOp.java create mode 100644 testAPI/src/main/java/com/bonitaSoft/tools/LocalStorage.java create mode 100644 testAPI/src/main/java/com/bonitaSoft/tools/LoginInfo.java create mode 100644 testAPI/src/main/java/com/bonitaSoft/tools/ServletContextClass.java delete mode 100644 testAPI/src/main/java/com/bonotaSoft/testAPI/MyResource.java delete mode 100644 testAPI/src/main/java/com/bonotaSoft/testAPI/test.java create mode 100644 testAPI/src/main/resources/config.properties diff --git a/testAPI/pom.xml b/testAPI/pom.xml index a5ceee5..bb2d5ac 100644 --- a/testAPI/pom.xml +++ b/testAPI/pom.xml @@ -55,6 +55,18 @@ 20140107 + + javax.servlet + servlet-api + 2.5 + + + + org.bonitasoft.engine + bonita-client + 6.3.4 + + diff --git a/testAPI/src/main/java/com/bonitaSoft/business/SystemOpImpl.java b/testAPI/src/main/java/com/bonitaSoft/business/SystemOpImpl.java new file mode 100644 index 0000000..c20d02d --- /dev/null +++ b/testAPI/src/main/java/com/bonitaSoft/business/SystemOpImpl.java @@ -0,0 +1,62 @@ +package com.bonitaSoft.business; + +import java.io.IOException; +import java.sql.Date; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.ServletContext; + +import org.bonitasoft.engine.api.ApiAccessType; +import org.bonitasoft.engine.api.LoginAPI; +import org.bonitasoft.engine.api.TenantAPIAccessor; +import org.bonitasoft.engine.exception.BonitaHomeNotSetException; +import org.bonitasoft.engine.exception.ServerAPIException; +import org.bonitasoft.engine.exception.UnknownAPITypeException; +import org.bonitasoft.engine.platform.LoginException; +import org.bonitasoft.engine.session.APISession; +import org.bonitasoft.engine.util.APITypeManager; + +import com.bonitaSoft.tools.LocalStorage; +import com.bonitaSoft.tools.LoginInfo; +import com.bonitaSoft.tools.ReturnObject; +import com.bonitaSoft.tools.Tools; + +public class SystemOpImpl { + + Tools myTools = new Tools(); + + public void getBusiness(ReturnObject myJSONObject, HashMap inputs, ServletContext context) throws IOException { + String log = (String) inputs.get("log"); + String pass = (String) inputs.get("pass"); + + Map map = new HashMap(); + map.put("server.url", "http://localhost:8080"); + map.put("application.name", "bonita"); + APITypeManager.setAPITypeAndParams(ApiAccessType.HTTP, map); + // get the LoginAPI using the TenantAPIAccessor + try { + final LoginAPI loginAPI = TenantAPIAccessor.getLoginAPI(); + + final APISession apiSession = loginAPI.login(log, pass); + + String token = myTools.generateToken(); + + Date resultdate = new Date(System.currentTimeMillis()); + + LoginInfo loginInfo = new LoginInfo(log,pass,apiSession,resultdate,token); + + LocalStorage localStorage = (LocalStorage) context.getAttribute("localStorage"); + + localStorage.setStorage(token, loginInfo); + + myJSONObject.addDatas("token", token); + } catch (BonitaHomeNotSetException | ServerAPIException | UnknownAPITypeException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (LoginException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/testAPI/src/main/java/com/bonitaSoft/business/TestImpl.java b/testAPI/src/main/java/com/bonitaSoft/business/TestImpl.java new file mode 100644 index 0000000..e867322 --- /dev/null +++ b/testAPI/src/main/java/com/bonitaSoft/business/TestImpl.java @@ -0,0 +1,18 @@ +package com.bonitaSoft.business; + +import java.io.IOException; +import java.util.HashMap; + +import com.bonitaSoft.tools.ReturnObject; + +public class TestImpl { + + public TestImpl(){ + + } + + public void getBusiness(ReturnObject myJSONObject, HashMap inputs) throws IOException { + myJSONObject.addDatas("truc", 1); + myJSONObject.addDatas("name", inputs.get("name")); + } +} diff --git a/testAPI/src/main/java/com/bonitaSoft/testAPI/SystemOp.java b/testAPI/src/main/java/com/bonitaSoft/testAPI/SystemOp.java new file mode 100644 index 0000000..7a2e997 --- /dev/null +++ b/testAPI/src/main/java/com/bonitaSoft/testAPI/SystemOp.java @@ -0,0 +1,94 @@ +package com.bonitaSoft.testAPI; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletContext; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.UriInfo; + +import org.json.JSONObject; + +import com.bonitaSoft.business.SystemOpImpl; +import com.bonitaSoft.tools.ReturnObject; +import com.bonitaSoft.tools.Tools; + +/** + * Root resource (exposed at "test" path) + * testAPI/webapi/systemop/login.php?log=walter.bates&pass=bpm + */ +@Path("/systemop") +public class SystemOp { + Tools myTools = new Tools(); + SystemOpImpl mySystemOpImpl = new SystemOpImpl(); + + @Context ServletContext context; + + Boolean debug = true; + + Boolean publicI = true; + + @GET + @Path("/login.php") + @Produces(MediaType.TEXT_PLAIN) + public String testGet(@Context UriInfo uriInfo) { + return execTest(uriInfo); + } + + @POST + @Path("/login.php") + @Produces(MediaType.TEXT_PLAIN) + public String testPost(@Context UriInfo uriInfo) { + return execTest(uriInfo); + } + + public String execTest(UriInfo uriInfo) { + JSONObject returnJSONObject = new JSONObject(); + try{ + //def + List inputsDef = new ArrayList(); + ReturnObject myJSONObject = new ReturnObject(); + + //input 'log' + Map log = new HashMap(); + log.put("label", "log"); + log.put("type", String.class); + log.put("default", null); + + inputsDef.add(log); + + //input 'pass' + Map pass = new HashMap(); + pass.put("label", "pass"); + pass.put("type", String.class); + pass.put("default", null); + + inputsDef.add(pass); + + //check input + HashMap inputs = myTools.checkInput(uriInfo, inputsDef, publicI, debug); + + if(inputs.get("crtInputs") == null) { + //------------------------------------------------ + //custom part + mySystemOpImpl.getBusiness(myJSONObject, inputs, context); + //------------------------------------------------ + }else{ + myJSONObject.setStrErreur((String) inputs.get("crtInputs")); + } + + returnJSONObject = myJSONObject.getReturnObjectJson(); + return returnJSONObject.toString(); + }catch (Exception e){ + myTools.traceLog("Error : " + e.getMessage()); + return returnJSONObject.toString(); + } + } +} diff --git a/testAPI/src/main/java/com/bonitaSoft/testAPI/Test.java b/testAPI/src/main/java/com/bonitaSoft/testAPI/Test.java index 75955d2..d1be1a8 100644 --- a/testAPI/src/main/java/com/bonitaSoft/testAPI/Test.java +++ b/testAPI/src/main/java/com/bonitaSoft/testAPI/Test.java @@ -1,14 +1,23 @@ package com.bonitaSoft.testAPI; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.servlet.ServletContext; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.UriInfo; import org.json.JSONObject; +import com.bonitaSoft.business.TestImpl; +import com.bonitaSoft.tools.LocalStorage; import com.bonitaSoft.tools.ReturnObject; import com.bonitaSoft.tools.Tools; @@ -16,34 +25,68 @@ /** * Root resource (exposed at "test" path) */ -@Path("test") +@Path("/test") public class Test { Tools myTools = new Tools(); - ReturnObject myJSONObject = new ReturnObject(); + TestImpl myTestImpl = new TestImpl(); + + @Context ServletContext context; + + Boolean debug = true; + + Boolean publicI = false; - /** - * Method handling HTTP GET requests. The returned object will be sent - * to the client as "text/plain" media type. - * - * @return String that will be returned as a text/plain response. - */ @GET + @Path("/unTest.php") @Produces(MediaType.TEXT_PLAIN) - public String testGet(@QueryParam("name") String name) { - return implTest(name); + public String testGet(@Context UriInfo uriInfo) { + return execTest(uriInfo); } @POST + @Path("/unTest.php") @Produces(MediaType.TEXT_PLAIN) - public String testPost(@QueryParam("name") String name) { - return implTest(name); + public String testPost(@Context UriInfo uriInfo) { + return execTest(uriInfo); } - public String implTest(String name) { + public String execTest(UriInfo uriInfo) { JSONObject returnJSONObject = new JSONObject(); try{ - myJSONObject.addDatas("truc", 1); - myJSONObject.addDatas("name", name); + LocalStorage localStorage = (LocalStorage) context.getAttribute("localStorage"); + myTools.traceLog("list : " + localStorage.getStorage("hello")); + + //def + List inputsDef = new ArrayList(); + ReturnObject myJSONObject = new ReturnObject(); + + //input 'name' + Map name = new HashMap(); + name.put("label", "name"); + name.put("type", String.class); + name.put("default", null); + + inputsDef.add(name); + + //input 'lab' + Map lab = new HashMap(); + lab.put("label", "lab"); + lab.put("type", Integer.class); + lab.put("default", 1); + + inputsDef.add(lab); + + //check input + HashMap inputs = myTools.checkInput(uriInfo, inputsDef, publicI, debug); + + if(inputs.get("crtInputs") == null) { + //------------------------------------------------ + //custom part + myTestImpl.getBusiness(myJSONObject, inputs); + //------------------------------------------------ + }else{ + myJSONObject.setStrErreur((String) inputs.get("crtInputs")); + } returnJSONObject = myJSONObject.getReturnObjectJson(); return returnJSONObject.toString(); diff --git a/testAPI/src/main/java/com/bonitaSoft/tools/LocalStorage.java b/testAPI/src/main/java/com/bonitaSoft/tools/LocalStorage.java new file mode 100644 index 0000000..01cadb3 --- /dev/null +++ b/testAPI/src/main/java/com/bonitaSoft/tools/LocalStorage.java @@ -0,0 +1,15 @@ +package com.bonitaSoft.tools; + +import java.util.HashMap; + +public class LocalStorage { + private HashMap storage = new HashMap(); + + public Object getStorage(String key) { + return storage.get(key); + } + + public void setStorage(String key, Object obj) { + this.storage.put(key, obj); + } +} diff --git a/testAPI/src/main/java/com/bonitaSoft/tools/LoginInfo.java b/testAPI/src/main/java/com/bonitaSoft/tools/LoginInfo.java new file mode 100644 index 0000000..f66d24d --- /dev/null +++ b/testAPI/src/main/java/com/bonitaSoft/tools/LoginInfo.java @@ -0,0 +1,73 @@ +package com.bonitaSoft.tools; + +import java.util.Date; + +public class LoginInfo { + private String user; + + private String pass; + + private Object session; + + private Date startDateSession; + + private String key; + + private Integer Ttl = 3600; + + public LoginInfo(String user, String pass, Object session, Date startDateSession, String key) { + this.user = user; + this.pass = pass; + this.session = session; + this.startDateSession = startDateSession; + this.key = key; + } + + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } + + public String getPass() { + return pass; + } + + public void setPass(String pass) { + this.pass = pass; + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public Integer getTtl() { + return Ttl; + } + + public void setTtl(Integer ttl) { + Ttl = ttl; + } + + public Object getSession() { + return session; + } + + public void setSession(Object session) { + this.session = session; + } + + public Date getStartDateSession() { + return startDateSession; + } + + public void setStartDateSession(Date startDateSession) { + this.startDateSession = startDateSession; + } +} diff --git a/testAPI/src/main/java/com/bonitaSoft/tools/ServletContextClass.java b/testAPI/src/main/java/com/bonitaSoft/tools/ServletContextClass.java new file mode 100644 index 0000000..bf9391a --- /dev/null +++ b/testAPI/src/main/java/com/bonitaSoft/tools/ServletContextClass.java @@ -0,0 +1,24 @@ +package com.bonitaSoft.tools; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import com.bonitaSoft.tools.LocalStorage; + +public class ServletContextClass implements ServletContextListener +{ + @Override + public void contextInitialized(ServletContextEvent sce) { + //add to ServletContext + LocalStorage localStorage = new LocalStorage(); + + localStorage.setStorage("hello", "Hello World"); + + sce.getServletContext().setAttribute("localStorage", localStorage); + } + + + @Override + public void contextDestroyed(ServletContextEvent sce) { + // TODO Auto-generated method stub + } +} diff --git a/testAPI/src/main/java/com/bonitaSoft/tools/Tools.java b/testAPI/src/main/java/com/bonitaSoft/tools/Tools.java index 33614a7..e58f5ac 100644 --- a/testAPI/src/main/java/com/bonitaSoft/tools/Tools.java +++ b/testAPI/src/main/java/com/bonitaSoft/tools/Tools.java @@ -1,10 +1,21 @@ package com.bonitaSoft.tools; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.math.BigInteger; +import java.security.SecureRandom; import java.sql.Date; import java.text.SimpleDateFormat; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Properties; import java.util.logging.Logger; -import com.bonitaSoft.testAPI.Test; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.UriInfo; public class Tools { @@ -24,5 +35,85 @@ public void traceLog(String p_msg){ System.out.println(outMsg); LOGGER.severe(outMsg); } + + public HashMap checkInput(UriInfo uriInfo, List inputsDef, Boolean publicI, Boolean debug) throws Exception{ + + String strError = ""; + + + MultivaluedMap queryParams = uriInfo.getQueryParameters(); + HashMap returnInput = new HashMap(); + + if(!publicI){ + String key = this.getPropertie("config.properties", "keyExamlple"); + + String inputKey = queryParams.getFirst("keyI"); + + if(!key.equals(inputKey)){ + strError = "KeyI invalid."; + returnInput.put("crtInputs", strError); + return returnInput; + } + } + + strError = "Error missing intput"; + + for (Iterator i = inputsDef.iterator(); i.hasNext();){ + @SuppressWarnings("unchecked") + Map item = (Map) i.next(); + String label = (String) item.get("label"); + Class type = (Class) item.get("type"); + Object defaultValue = type.cast(item.get("default")); + + if(defaultValue == null){ + Object value = type.cast(queryParams.getFirst(label)); + if(value == null){ + String strMsg = "," + label; + if(debug) + this.traceLog(strMsg); + strError += strMsg; + }else{ + returnInput.put(label, value); + if(debug) + this.traceLog("required value-"+label+" : " + value.toString()); + } + }else{ + Object value = type.cast(queryParams.getFirst(label)); + if(value == null){ + value = defaultValue; + } + returnInput.put(label, value); + if(debug) + this.traceLog("option value-"+label+" : " + value.toString()); + } + + if(strError != "Error missing intput") + returnInput.put("crtInputs", strError); + } + + return returnInput; + } + + public String getPropertie(String file, String propertie) throws IOException { + String returnPropertie = null; + + Properties prop = new Properties(); + + InputStream inputStream = getClass().getClassLoader().getResourceAsStream(file); + prop.load(inputStream); + if (inputStream == null) { + throw new FileNotFoundException("property file '" + file + "' not found in the classpath"); + } + + // get the property value and print it out + returnPropertie = prop.getProperty(propertie); + + return returnPropertie; + } + + public String generateToken(){ + SecureRandom random = new SecureRandom(); + return new BigInteger(130, random).toString(15); + } } diff --git a/testAPI/src/main/java/com/bonotaSoft/testAPI/MyResource.java b/testAPI/src/main/java/com/bonotaSoft/testAPI/MyResource.java deleted file mode 100644 index f3a1922..0000000 --- a/testAPI/src/main/java/com/bonotaSoft/testAPI/MyResource.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.bonotaSoft.testAPI; - -import javax.json.Json; -import javax.json.JsonObject; -import javax.ws.rs.GET; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; - -/** - * Root resource (exposed at "myresource" path) - */ -@Path("myresource") -public class MyResource { - - /** - * Method handling HTTP GET requests. The returned object will be sent - * to the client as "text/plain" media type. - * - * @return String that will be returned as a text/plain response. - */ - @GET - @Produces(MediaType.TEXT_PLAIN) - public String getIt() { - JsonObject myObject = Json.createObjectBuilder() - .add("name", "Agamemnon") - .add("age", 32) - .build(); - - return myObject.toString(); - } -} diff --git a/testAPI/src/main/java/com/bonotaSoft/testAPI/test.java b/testAPI/src/main/java/com/bonotaSoft/testAPI/test.java deleted file mode 100644 index 4dc85f8..0000000 --- a/testAPI/src/main/java/com/bonotaSoft/testAPI/test.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.bonotaSoft.testAPI; - -import javax.json.Json; -import javax.json.JsonObject; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.MediaType; - -/** - * Root resource (exposed at "test" path) - */ -@Path("test") -public class test { - - /** - * Method handling HTTP GET requests. The returned object will be sent - * to the client as "text/plain" media type. - * - * @return String that will be returned as a text/plain response. - */ - @GET - @Produces(MediaType.APPLICATION_JSON) - public JsonObject testGet(@QueryParam("name") String name) { - return implTest(name); - } - - @POST - @Produces(MediaType.APPLICATION_JSON) - public JsonObject testPost(@QueryParam("name") String name) { - return implTest(name); - } - - public JsonObject implTest(String name) { - JsonObject myObject = Json.createObjectBuilder() - .add("name", name) - .add("age", 32) - .build(); - - return myObject; - } -} diff --git a/testAPI/src/main/resources/config.properties b/testAPI/src/main/resources/config.properties new file mode 100644 index 0000000..1798a79 --- /dev/null +++ b/testAPI/src/main/resources/config.properties @@ -0,0 +1,3 @@ +#Properties +user=happy +keyExamlple=efsdf5s4df7sd5f437sdf \ No newline at end of file diff --git a/testAPI/src/main/webapp/WEB-INF/web.xml b/testAPI/src/main/webapp/WEB-INF/web.xml index e60d804..00fdce0 100644 --- a/testAPI/src/main/webapp/WEB-INF/web.xml +++ b/testAPI/src/main/webapp/WEB-INF/web.xml @@ -15,4 +15,7 @@ Jersey Web Application /webapi/* + + com.bonitaSoft.tools.ServletContextClass +