Author: fmui
Date: Fri Jan 28 14:11:25 2011
New Revision: 1064683

URL: http://svn.apache.org/viewvc?rev=1064683&view=rev
Log:
found a solution for the cross-domain problem

Removed:
    
incubator/chemistry/opencmis-browser-binding/trunk/src/main/webapp/result.html
Modified:
    
incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/BrowserBindingUtils.java
    
incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.java
    
incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/ObjectService.java
    
incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/RepositoryService.java
    
incubator/chemistry/opencmis-browser-binding/trunk/src/main/webapp/create.html

Modified: 
incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/BrowserBindingUtils.java
URL: 
http://svn.apache.org/viewvc/incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/BrowserBindingUtils.java?rev=1064683&r1=1064682&r2=1064683&view=diff
==============================================================================
--- 
incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/BrowserBindingUtils.java
 (original)
+++ 
incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/BrowserBindingUtils.java
 Fri Jan 28 14:11:25 2011
@@ -31,6 +31,7 @@ import java.util.List;
 import java.util.Locale;
 import java.util.Map;
 
+import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
@@ -44,6 +45,7 @@ import org.apache.chemistry.opencmis.com
 import org.apache.chemistry.opencmis.commons.definitions.TypeDefinition;
 import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
 import 
org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
+import org.apache.chemistry.opencmis.commons.impl.Base64;
 import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
 import 
org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
 import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertiesImpl;
@@ -59,6 +61,7 @@ import org.apache.chemistry.opencmis.com
 import org.apache.chemistry.opencmis.commons.server.CmisService;
 import org.apache.chemistry.opencmis.server.impl.CallContextImpl;
 import org.apache.chemistry.opencmis.server.shared.HttpUtils;
+import org.json.simple.JSONObject;
 import org.json.simple.JSONStreamAware;
 
 public class BrowserBindingUtils {
@@ -67,6 +70,8 @@ public class BrowserBindingUtils {
 
     public static final String ROOT_PATH_FRAGMENT = "root";
 
+    public static final String SELECTOR_LAST_RESULT = "lastResult";
+
     public static final String SELECTOR_TYPE_CHILDREN = "typeChildren";
     public static final String SELECTOR_TYPE_DESCENDANTS = "typeDescendants";
     public static final String SELECTOR_TYPE_DEFINITION = "typeDefintion";
@@ -84,10 +89,11 @@ public class BrowserBindingUtils {
     public static final String ACTION_QUERY = "query";
 
     public static final String PARAM_SELECTOR = "selector";
-    public static final String PARAM_REDIRECT = "redirect";
+    public static final String PARAM_TRANSACTION = "transaction";
     public static final String PARAM_CLIENTTOKEN = "clientToken";
 
     public static final String FIELD_ACTION = "cmisaction";
+    public static final String FIELD_TRANSACTION = "transaction";
     public static final String FIELD_OBJECT_ID = "objectId";
     public static final String FIELD_PROP_NAME = "property_name_";
     public static final String FIELD_PROP_VALUE = "property_value_";
@@ -95,6 +101,7 @@ public class BrowserBindingUtils {
     public static final String CONTEXT_OBJECT_ID = 
"org.apache.chemistry.openmis.browserbinding.objectId";
     public static final String CONTEXT_OBJECT_TYPE_ID = 
"org.apache.chemistry.openmis.browserbinding.objectTypeId";
     public static final String CONTEXT_BASETYPE_ID = 
"org.apache.chemistry.openmis.browserbinding.basetypeId";
+    public static final String CONTEXT_TRANSACTION = 
"org.apache.chemistry.openmis.browserbinding.transaction";
 
     public enum CallUrl {
         SERVICE, REPOSITORY, ROOT
@@ -149,7 +156,7 @@ public class BrowserBindingUtils {
      * Returns the object id of the current request.
      */
     public static void prepareContext(CallContext context, CallUrl callUrl, 
CmisService service, String repositoryId,
-            String objectId, HttpServletRequest request) {
+            String objectId, String transaction, HttpServletRequest request) {
         if (callUrl != CallUrl.ROOT) {
             return;
         }
@@ -170,6 +177,7 @@ public class BrowserBindingUtils {
                     getProperty(object, PropertyIds.OBJECT_TYPE_ID, 
String.class));
             ((CallContextImpl) context).put(CONTEXT_BASETYPE_ID,
                     getProperty(object, PropertyIds.BASE_TYPE_ID, 
String.class));
+            ((CallContextImpl) context).put(CONTEXT_TRANSACTION, transaction);
         }
     }
 
@@ -333,6 +341,57 @@ public class BrowserBindingUtils {
     }
 
     /**
+     * Transforms the transaction into a cookie name.
+     */
+    public static String getCookieName(String transaction) {
+        if ((transaction == null) || (transaction.length() == 0)) {
+            return "cmis%";
+        }
+      
+        return "cmis_" + 
Base64.encodeBytes(transaction.getBytes()).replace('=', '%');
+    }
+
+    /**
+     * Sets a transaction cookie.
+     */
+    public static void setCookie(HttpServletRequest request, 
HttpServletResponse response, String repositoryId,
+            String transaction, String value) {
+        setCookie(request, response, repositoryId, transaction, value, 3600);
+    }
+
+    /**
+     * Deletes a transaction cookie.
+     */
+    public static void deleteCookie(HttpServletRequest request, 
HttpServletResponse response, String repositoryId,
+            String transaction) {
+        setCookie(request, response, repositoryId, transaction, "", 0);
+    }
+
+    /**
+     * Sets a transaction cookie.
+     */
+    public static void setCookie(HttpServletRequest request, 
HttpServletResponse response, String repositoryId,
+            String transaction, String value, int expiry) {
+        if ((transaction != null) && (transaction.length() > 0)) {
+            Cookie tansactionCookie = new 
Cookie(BrowserBindingUtils.getCookieName(transaction), value);
+            tansactionCookie.setMaxAge(expiry);
+            tansactionCookie.setPath(request.getContextPath() + 
request.getServletPath() + "/" + repositoryId);
+            response.addCookie(tansactionCookie);
+        }
+    }
+    
+    @SuppressWarnings("unchecked")
+    public static String createCookieValue(int code, String objectId, String 
error) {
+        JSONObject result = new JSONObject();
+        
+        result.put("code", code);
+        result.put("objectId", objectId);
+        result.put("error", error);
+        
+        return result.toJSONString();
+    }
+
+    /**
      * Writes JSON to the servlet response and adds a callback wrapper if
      * requested.
      */

Modified: 
incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.java
URL: 
http://svn.apache.org/viewvc/incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.java?rev=1064683&r1=1064682&r2=1064683&view=diff
==============================================================================
--- 
incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.java
 (original)
+++ 
incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/CmisBrowserBindingServlet.java
 Fri Jan 28 14:11:25 2011
@@ -90,6 +90,8 @@ public class CmisBrowserBindingServlet e
 
         try {
             repositoryDispatcher.addResource("", Dispatcher.METHOD_GET, 
RepositoryService.class, "getRepositoryInfo");
+            
repositoryDispatcher.addResource(BrowserBindingUtils.SELECTOR_LAST_RESULT, 
Dispatcher.METHOD_GET,
+                    RepositoryService.class, "getLastResult");
             
repositoryDispatcher.addResource(BrowserBindingUtils.SELECTOR_TYPE_CHILDREN, 
Dispatcher.METHOD_GET,
                     RepositoryService.class, "getTypeChildren");
             
repositoryDispatcher.addResource(BrowserBindingUtils.SELECTOR_TYPE_DESCENDANTS, 
Dispatcher.METHOD_GET,
@@ -140,13 +142,13 @@ public class CmisBrowserBindingServlet e
                     response.setHeader("WWW-Authenticate", "Basic 
realm=\"CMIS\"");
                     response.sendError(HttpServletResponse.SC_UNAUTHORIZED, 
"Authorization Required");
                 } else {
-                    writeError((CmisBaseException) e, request, response);
+                    writeError((CmisBaseException) e, request, response, 
context);
                 }
             } else if (e instanceof CmisRuntimeException) {
                 LOG.error(e.getMessage(), e);
-                writeError((CmisBaseException) e, request, response);
+                writeError((CmisBaseException) e, request, response, context);
             } else if (e instanceof CmisBaseException) {
-                writeError((CmisBaseException) e, request, response);
+                writeError((CmisBaseException) e, request, response, context);
             } else {
                 LOG.error(e.getMessage(), e);
                 
response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, 
e.getMessage());
@@ -205,7 +207,7 @@ public class CmisBrowserBindingServlet e
                 String objectId = getStringParameter(request, 
Constants.PARAM_OBJECT_ID);
 
                 // add object id and object base type id to context
-                BrowserBindingUtils.prepareContext(context, callUrl, service, 
repositoryId, objectId, request);
+                BrowserBindingUtils.prepareContext(context, callUrl, service, 
repositoryId, objectId, null, request);
 
                 // dispatch
                 if (callUrl == CallUrl.REPOSITORY) {
@@ -245,13 +247,15 @@ public class CmisBrowserBindingServlet e
 
                 String action = HttpUtils.getStringParameter(postRequest, 
BrowserBindingUtils.FIELD_ACTION);
                 String objectId = HttpUtils.getStringParameter(postRequest, 
BrowserBindingUtils.FIELD_OBJECT_ID);
+                String transaction = HttpUtils.getStringParameter(postRequest, 
BrowserBindingUtils.FIELD_TRANSACTION);
 
                 if ((action == null) || (action.length() == 0)) {
                     throw new CmisNotSupportedException("Unknown action");
                 }
 
                 // add object id and object base type id to context
-                BrowserBindingUtils.prepareContext(context, callUrl, service, 
repositoryId, objectId, postRequest);
+                BrowserBindingUtils.prepareContext(context, callUrl, service, 
repositoryId, objectId, transaction,
+                        postRequest);
 
                 // dispatch
                 if (callUrl == CallUrl.REPOSITORY) {
@@ -279,7 +283,8 @@ public class CmisBrowserBindingServlet e
      * Translates an exception in an appropriate HTTP error code.
      */
     @SuppressWarnings("unchecked")
-    private void writeError(CmisBaseException ex, HttpServletRequest request, 
HttpServletResponse response) {
+    private void writeError(CmisBaseException ex, HttpServletRequest request, 
HttpServletResponse response,
+            CallContext context) {
         int code = 500;
 
         if (ex instanceof CmisConstraintException) {
@@ -310,6 +315,11 @@ public class CmisBrowserBindingServlet e
 
         response.setStatus(code);
         response.setContentType(BrowserBindingUtils.JSON_MIME_TYPE);
+        if (context != null) {
+            BrowserBindingUtils.setCookie(request, response, 
context.getRepositoryId(),
+                    (String) 
context.get(BrowserBindingUtils.CONTEXT_TRANSACTION),
+                    BrowserBindingUtils.createCookieValue(code, null, 
ex.getMessage()));
+        }
 
         JSONObject jsonResponse = new JSONObject();
         JSONObject jsonResponseObject = new JSONObject();

Modified: 
incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/ObjectService.java
URL: 
http://svn.apache.org/viewvc/incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/ObjectService.java?rev=1064683&r1=1064682&r2=1064683&view=diff
==============================================================================
--- 
incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/ObjectService.java
 (original)
+++ 
incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/ObjectService.java
 Fri Jan 28 14:11:25 2011
@@ -27,8 +27,6 @@ import java.io.BufferedOutputStream;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.math.BigInteger;
-import java.net.MalformedURLException;
-import java.net.URL;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -37,11 +35,9 @@ import org.apache.chemistry.opencmis.com
 import org.apache.chemistry.opencmis.commons.data.ObjectData;
 import org.apache.chemistry.opencmis.commons.enums.IncludeRelationships;
 import org.apache.chemistry.opencmis.commons.enums.VersioningState;
-import 
org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException;
 import org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException;
 import org.apache.chemistry.opencmis.commons.impl.Constants;
 import org.apache.chemistry.opencmis.commons.impl.ReturnVersion;
-import org.apache.chemistry.opencmis.commons.impl.UrlBuilder;
 import org.apache.chemistry.opencmis.commons.server.CallContext;
 import org.apache.chemistry.opencmis.commons.server.CmisService;
 import org.apache.chemistry.opencmis.commons.server.ObjectInfo;
@@ -61,45 +57,33 @@ public final class ObjectService {
         String folderId = (String) 
context.get(BrowserBindingUtils.CONTEXT_OBJECT_ID);
         VersioningState versioningState = getEnumParameter(request, 
Constants.PARAM_VERSIONIG_STATE,
                 VersioningState.class);
-        String redirect = getStringParameter(request, 
BrowserBindingUtils.PARAM_REDIRECT);
-        URL redirectUrl = null;
-        if (redirect != null) {
-            try {
-                redirectUrl = new URL(redirect);
-            } catch (MalformedURLException e) {
-                throw new CmisInvalidArgumentException("Redirect URL is 
invalid!");
-            }
-        }
+        String transaction = getStringParameter(request, 
BrowserBindingUtils.PARAM_TRANSACTION);
 
         TypeCache typeCache = new TypeCache(repositoryId, service);
 
         String newObjectId = service.createDocument(repositoryId,
-                BrowserBindingUtils.createProperties(request,null, typeCache), 
folderId,
+                BrowserBindingUtils.createProperties(request, null, 
typeCache), folderId,
                 BrowserBindingUtils.createContentStream(request), 
versioningState,
                 BrowserBindingUtils.createPolcies(request), 
BrowserBindingUtils.createAddAcl(request),
                 BrowserBindingUtils.createRemoveAcl(request), null);
 
-        if (redirectUrl != null) {
-            UrlBuilder ub= new UrlBuilder(redirectUrl.toString());
-            ub.addParameter("cmis:objectId", newObjectId);
-            
-            response.sendRedirect(ub.toString());
-        } else {
-            ObjectInfo objectInfo = service.getObjectInfo(repositoryId, 
newObjectId);
-            if (objectInfo == null) {
-                throw new CmisRuntimeException("Object Info is missing!");
-            }
-
-            ObjectData object = objectInfo.getObject();
-            if (object == null) {
-                throw new CmisRuntimeException("Object is null!");
-            }
-
-            JSONObject jsonObject = JSONConverter.convert(object, typeCache);
+        ObjectInfo objectInfo = service.getObjectInfo(repositoryId, 
newObjectId);
+        if (objectInfo == null) {
+            throw new CmisRuntimeException("Object Info is missing!");
+        }
 
-            response.setStatus(HttpServletResponse.SC_OK);
-            BrowserBindingUtils.writeJSON(jsonObject, request, response);
+        ObjectData object = objectInfo.getObject();
+        if (object == null) {
+            throw new CmisRuntimeException("Object is null!");
         }
+
+        JSONObject jsonObject = JSONConverter.convert(object, typeCache);
+
+        response.setStatus(HttpServletResponse.SC_OK);
+        BrowserBindingUtils.setCookie(request, response, repositoryId, 
transaction,
+                BrowserBindingUtils.createCookieValue(200, object.getId(), 
null));
+
+        BrowserBindingUtils.writeJSON(jsonObject, request, response);
     }
 
     /**

Modified: 
incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/RepositoryService.java
URL: 
http://svn.apache.org/viewvc/incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/RepositoryService.java?rev=1064683&r1=1064682&r2=1064683&view=diff
==============================================================================
--- 
incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/RepositoryService.java
 (original)
+++ 
incubator/chemistry/opencmis-browser-binding/trunk/src/main/java/org/apache/chemistry/opencmis/server/impl/browser/RepositoryService.java
 Fri Jan 28 14:11:25 2011
@@ -25,6 +25,7 @@ import static org.apache.chemistry.openc
 import java.math.BigInteger;
 import java.util.List;
 
+import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
@@ -39,6 +40,7 @@ import org.apache.chemistry.opencmis.com
 import org.apache.chemistry.opencmis.server.impl.browser.json.JSONConverter;
 import org.json.simple.JSONArray;
 import org.json.simple.JSONObject;
+import org.json.simple.JSONValue;
 
 public final class RepositoryService {
 
@@ -73,6 +75,43 @@ public final class RepositoryService {
     }
 
     /**
+     * getLastResult.
+     */
+    @SuppressWarnings("unchecked")
+    public static void getLastResult(CallContext context, CmisService service, 
String repositoryId,
+            HttpServletRequest request, HttpServletResponse response) throws 
Exception {
+
+        String transaction = getStringParameter(request, 
BrowserBindingUtils.PARAM_TRANSACTION);
+        String cookieName = BrowserBindingUtils.getCookieName(transaction);
+        String cookieValue = null;
+
+        if (request.getCookies() != null) {
+            for (Cookie cookie : request.getCookies()) {
+                if (cookieName.equals(cookie.getName())) {
+                    cookieValue = cookie.getValue();
+                    break;
+                }
+            }
+        }
+
+        JSONObject result = null;
+        try {
+            if (cookieValue == null) {
+                cookieValue = BrowserBindingUtils.createCookieValue(0, null, 
"Unknown transaction!");
+            }
+
+            result = (JSONObject) JSONValue.parse(cookieValue);
+        } catch (Exception pe) {
+            result.put("code", 0);
+            result.put("objectId", null);
+            result.put("error", "Cookie pasring error!");
+        }
+
+        response.setStatus(HttpServletResponse.SC_OK);
+        BrowserBindingUtils.writeJSON((JSONObject) 
JSONValue.parse(cookieValue), request, response);
+    }
+
+    /**
      * getTypeChildren.
      */
     public static void getTypeChildren(CallContext context, CmisService 
service, String repositoryId,

Modified: 
incubator/chemistry/opencmis-browser-binding/trunk/src/main/webapp/create.html
URL: 
http://svn.apache.org/viewvc/incubator/chemistry/opencmis-browser-binding/trunk/src/main/webapp/create.html?rev=1064683&r1=1064682&r2=1064683&view=diff
==============================================================================
--- 
incubator/chemistry/opencmis-browser-binding/trunk/src/main/webapp/create.html 
(original)
+++ 
incubator/chemistry/opencmis-browser-binding/trunk/src/main/webapp/create.html 
Fri Jan 28 14:11:25 2011
@@ -22,6 +22,7 @@ td {
 <script type="text/javascript">
 var repositoryUrl;
 var rootFolderUrl;
+var lastTransaction;
 
 function loadRepositoryInfos(infos) {
     for(repId in infos) {
@@ -41,10 +42,31 @@ function createDocument() {
     document.getElementById('info').innerHTML = "Creating " + docname + " ...";
 
     createForm.action = rootFolderUrl + createForm.folder.value;
-    createForm.redirect.value = location.href.substring(0, 
location.href.lastIndexOf('/')) + "/result.html"
+    lastTransaction = (new Date()).getTime() + "-" + 
Math.floor(Math.random()*10000000) + "-" + docname;
+    createForm.transaction.value = lastTransaction;
  
     return true;
 }
+
+function createDocumentCallback() {
+       if(lastTransaction) {
+               document.getElementById('info').innerHTML = 'Transaction: ' + 
lastTransaction
+
+               var script1 = document.createElement("script");
+               script1.setAttribute("src", repositoryUrl + 
"?selector=lastResult&clientToken=showNewDocumentId&transaction="+lastTransaction);
+               script1.setAttribute("type","text/javascript");                
+               document.body.appendChild(script1);
+       }
+}
+
+function showNewDocumentId(result) {
+       if(result.objectId) {
+               alert("New document id: " + result.objectId + " (code: " + 
result.code + ")");
+       }
+       else {
+               alert("Error: " + result.error + " (code: " + result.code + 
")");
+       }
+}
 </script>
 </head>
 <body>
@@ -54,7 +76,7 @@ function createDocument() {
 
 <form id="createForm" action="" method="POST" target="result" 
enctype="multipart/form-data" onsubmit="return createDocument()">
 <input name="cmisaction" type="hidden" value="createDocument" />
-<input name="redirect" type="hidden" value="" />
+<input name="transaction" type="hidden" value="" />
 <table>
 <tr>
     <td>Folder:</td>
@@ -82,7 +104,7 @@ function createDocument() {
 </form>
 
 <div id="info"></div>
-<iframe name="result" style="border:2px;width:600px;height:400px;"></iframe>
+<iframe name="result" style="border:2px;width:600px;height:400px;" 
onload="createDocumentCallback()"></iframe>
 
 <script type="text/javascript" 
src="browser?clientToken=loadRepositoryInfos"></script>
 </body>


Reply via email to