Skip to content

Commit

Permalink
inputs, localstorage, connection bonitabpm
Browse files Browse the repository at this point in the history
  • Loading branch information
Happykiller committed Sep 9, 2014
1 parent 2ad4fa0 commit 716b741
Show file tree
Hide file tree
Showing 13 changed files with 455 additions and 93 deletions.
12 changes: 12 additions & 0 deletions testAPI/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,18 @@
<version>20140107</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>

<dependency>
<groupId>org.bonitasoft.engine</groupId>
<artifactId>bonita-client</artifactId>
<version>6.3.4</version>
</dependency>

</dependencies>

<properties>
Expand Down
62 changes: 62 additions & 0 deletions testAPI/src/main/java/com/bonitaSoft/business/SystemOpImpl.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> inputs, ServletContext context) throws IOException {
String log = (String) inputs.get("log");
String pass = (String) inputs.get("pass");

Map<String, String> map = new HashMap<String, String>();
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();
}
}
}
18 changes: 18 additions & 0 deletions testAPI/src/main/java/com/bonitaSoft/business/TestImpl.java
Original file line number Diff line number Diff line change
@@ -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<String, Object> inputs) throws IOException {
myJSONObject.addDatas("truc", 1);
myJSONObject.addDatas("name", inputs.get("name"));
}
}
94 changes: 94 additions & 0 deletions testAPI/src/main/java/com/bonitaSoft/testAPI/SystemOp.java
Original file line number Diff line number Diff line change
@@ -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<Object> inputsDef = new ArrayList<Object>();
ReturnObject myJSONObject = new ReturnObject();

//input 'log'
Map<String, Object> log = new HashMap<String, Object>();
log.put("label", "log");
log.put("type", String.class);
log.put("default", null);

inputsDef.add(log);

//input 'pass'
Map<String, Object> pass = new HashMap<String, Object>();
pass.put("label", "pass");
pass.put("type", String.class);
pass.put("default", null);

inputsDef.add(pass);

//check input
HashMap<String, Object> 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();
}
}
}
75 changes: 59 additions & 16 deletions testAPI/src/main/java/com/bonitaSoft/testAPI/Test.java
Original file line number Diff line number Diff line change
@@ -1,49 +1,92 @@
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;


/**
* 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<Object> inputsDef = new ArrayList<Object>();
ReturnObject myJSONObject = new ReturnObject();

//input 'name'
Map<String, Object> name = new HashMap<String, Object>();
name.put("label", "name");
name.put("type", String.class);
name.put("default", null);

inputsDef.add(name);

//input 'lab'
Map<String, Object> lab = new HashMap<String, Object>();
lab.put("label", "lab");
lab.put("type", Integer.class);
lab.put("default", 1);

inputsDef.add(lab);

//check input
HashMap<String, Object> 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();
Expand Down
15 changes: 15 additions & 0 deletions testAPI/src/main/java/com/bonitaSoft/tools/LocalStorage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.bonitaSoft.tools;

import java.util.HashMap;

public class LocalStorage {
private HashMap<String, Object> storage = new HashMap<String, Object>();

public Object getStorage(String key) {
return storage.get(key);
}

public void setStorage(String key, Object obj) {
this.storage.put(key, obj);
}
}
73 changes: 73 additions & 0 deletions testAPI/src/main/java/com/bonitaSoft/tools/LoginInfo.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit 716b741

Please sign in to comment.