Permalink
Browse files

Update samples for cacoo.com api update on 2012.09.27

- Use editor token function
- use editor automation function
  • Loading branch information...
Makoto Hirayama
Makoto Hirayama committed Sep 27, 2012
1 parent 4be7f5f commit d01767ce2063d786341168ed7599ee65585cd5b1
@@ -35,7 +35,7 @@
<tr>
<td><a href="detail?diagramId=<%=d.get("diagramId")%>"><%=d.get("title")%></a></td>
<td>
- <span class="action"><a href="<%=CacooUtils.editorLink((String)d.get("diagramId"), request)%>">EDIT</a></span>
+ <span class="action"><a href="editor?diagramId=<%=d.get("diagramId")%>">EDIT</a></span>
<span class="action"><a href="copyInit?diagramId=<%=d.get("diagramId")%>">COPY</a></span>
<span class="action"><a href="delete?diagramId=<%=d.get("diagramId")%>">DELETE</a></span>
</td>
@@ -56,6 +56,10 @@
<servlet-name>image</servlet-name>
<servlet-class>com.cacoo.apisample.cacoo.servlet.CacooImageServlet</servlet-class>
</servlet>
+ <servlet>
+ <servlet-name>editor</servlet-name>
+ <servlet-class>com.cacoo.apisample.cacoo.servlet.CacooEditorServlet</servlet-class>
+ </servlet>
<servlet-mapping>
<servlet-name>welcome</servlet-name>
@@ -97,6 +101,10 @@
<servlet-name>image</servlet-name>
<url-pattern>/cacoo/image</url-pattern>
</servlet-mapping>
+ <servlet-mapping>
+ <servlet-name>editor</servlet-name>
+ <url-pattern>/cacoo/editor</url-pattern>
+ </servlet-mapping>
<display-name>CacooApiSample</display-name>
@@ -38,6 +38,7 @@
public static final String SESSION_OAUTH_CONSUMER = "SESSION_OAUTH_CONSUMER";
// Cacoo URL
+ // FIXME If you have private enterprise package, change this URL to your system.
public static final String CACOO_URL = "https://cacoo.com/";
private static final String ACCESS_TOKEN_URL = CACOO_URL+"oauth/access_token";
@@ -131,7 +132,7 @@ public static OAuthToken retrieveAccessToken(HttpServletRequest req, String oaut
/*
* URL for diagram editor.
*/
- public static String editorLink(String diagramId, HttpServletRequest req){
+ public static String editorLink(String diagramId, String editorToken, String automationToken, HttpServletRequest req){
// FIXME Please specify the url to the "callbackURL" when users close editor page.
JsonObject root = new JsonObject();
root.addProperty("callbackUrl", createRequestHost(req)+req.getContextPath()+"/cacoo/");
@@ -142,7 +143,14 @@ public static String editorLink(String diagramId, HttpServletRequest req){
buttons.add(closeButton);
root.add("buttons",buttons);
try{
- return CACOO_URL+"diagrams/"+diagramId+"/edit?parameter="+URLEncoder.encode(root.toString(),"UTF-8");
+ String url = CACOO_URL+"diagrams/"+diagramId+"/edit?parameter="+URLEncoder.encode(root.toString(),"UTF-8");
+ if (editorToken != null && editorToken.length() > 0) {
+ url += "&editorToken="+URLEncoder.encode(editorToken, "UTF-8");
+ }
+ if (automationToken != null && automationToken.length() > 0) {
+ url += "&automationToken="+URLEncoder.encode(automationToken, "UTF-8");
+ }
+ return url;
}catch (Exception e) {
throw new RuntimeException(e);
}
@@ -16,6 +16,7 @@
/*
* Create a new diagram and redirect to the edit page.
* https://cacoo.com/api_create_diagram
+ * https://cacoo.com/api_editor_token
*/
public class CacooCreateServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@@ -24,17 +25,32 @@
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
try{
- HttpResponse res = CacooUtils.cacooApi(req, resp, "api/v1/diagrams/create.json", null);
+ HttpResponse res;
+ JsonElement json;
+ JsonObject root;
+
+ // step-1 : create diagram
+ res = CacooUtils.cacooApi(req, resp, "api/v1/diagrams/create.json", null);
if(res==null){
// OAuth authentication is required.
return;
}
- JsonElement json = CacooUtils.httpResponse2json(res);
- JsonObject root = json.getAsJsonObject();
+ json = CacooUtils.httpResponse2json(res);
+ root = json.getAsJsonObject();
String diagramId = root.get("diagramId").getAsString();
- // Redirect to editing page.
- resp.sendRedirect(CacooUtils.editorLink(diagramId, req));
+ // step-2 : get authentication token to open editing page
+ res = CacooUtils.cacooApi(req, resp, "api/v1/diagrams/"+diagramId+"/editor/token.json", null);
+ if(res==null){
+ // OAuth authentication is required.
+ return;
+ }
+ json = CacooUtils.httpResponse2json(res);
+ root = json.getAsJsonObject();
+ String editorToken = root.get("token").getAsString();
+
+ // step-3 : redirect to editing page.
+ resp.sendRedirect(CacooUtils.editorLink(diagramId, editorToken, "", req));
}catch (Exception e) {
e.printStackTrace();
req.setAttribute("message", e.getMessage());
@@ -0,0 +1,71 @@
+package com.cacoo.apisample.cacoo.servlet;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.http.HttpResponse;
+
+import com.cacoo.apisample.cacoo.CacooUtils;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+
+/*
+ * Edit diagram (Automatically add image from URL)
+ * https://cacoo.com/api_editor_token
+ * https://cacoo.com/api_editor_automation
+ */
+public class CacooEditorServlet extends HttpServlet {
+ private static final long serialVersionUID = 1L;
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+ throws ServletException, IOException {
+ String diagramId = req.getParameter("diagramId");
+ try{
+ HttpResponse res;
+ JsonElement json;
+ JsonObject root;
+
+ // step-1 : get authentication token to open editing page
+ res = CacooUtils.cacooApi(req, resp, "api/v1/diagrams/"+diagramId+"/editor/token.json", null);
+ if(res==null){
+ // OAuth authentication is required.
+ return;
+ }
+ json = CacooUtils.httpResponse2json(res);
+ root = json.getAsJsonObject();
+ String editorToken = root.get("token").getAsString();
+
+ // step-2 : register automation command and get token
+ Map<String,String> params = new HashMap<String, String>();
+
+ // FIXME Replace URL that you want to insert image.
+ String imageUrl = "http://www.nulab.co.jp/";
+
+ params.put("command", "{\"operations\":[{\"type\":\"AddImageUrl\",\"parameter\":{\"url\":\""+imageUrl+"\",\"x\":10,\"y\":10}}]}");
+ res = CacooUtils.cacooApi(req, resp, "api/v1/diagrams/"+diagramId+"/editor/automation.json", params);
+ if(res==null){
+ // OAuth authentication is required.
+ return;
+ }
+ json = CacooUtils.httpResponse2json(res);
+ root = json.getAsJsonObject();
+ String automationToken = root.get("token").getAsString();
+
+ // step-3 : redirect to editing page.
+ resp.sendRedirect(CacooUtils.editorLink(diagramId, editorToken, automationToken, req));
+ }catch (Exception e) {
+ e.printStackTrace();
+ req.setAttribute("message", e.getMessage());
+ req
+ .getRequestDispatcher("/WEB-INF/jsp/cacoo/error.jsp")
+ .forward(req, resp);
+ }
+ }
+}

0 comments on commit d01767c

Please sign in to comment.