Author: tyrell
Date: Sat Jan 12 11:46:54 2008
New Revision: 12161

Log:

Adding mashup source loading directly from the physical files.

Modified:
   trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java
   trunk/mashup/java/modules/www/editor.jsp

Modified: 
trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java
==============================================================================
--- trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java   
(original)
+++ trunk/mashup/java/modules/core/src/org/wso2/mashup/utils/MashupUtils.java   
Sat Jan 12 11:46:54 2008
@@ -24,13 +24,13 @@
 import org.apache.commons.logging.LogFactory;
 import org.wso2.mashup.MashupConstants;
 import org.wso2.mashup.MashupFault;
+import org.wso2.usermanager.UserManagerException;
+import org.wso2.utils.ServerConfiguration;
+import org.wso2.utils.security.CryptoUtil;
 import org.wso2.wsas.ServerConstants;
 import org.wso2.wsas.ServerManager;
 import org.wso2.wsas.persistence.PersistenceManager;
 import org.wso2.wsas.persistence.dataobject.ServiceUserDO;
-import org.wso2.utils.ServerConfiguration;
-import org.wso2.utils.security.CryptoUtil;
-import org.wso2.usermanager.UserManagerException;
 
 import javax.management.InstanceNotFoundException;
 import javax.management.MBeanException;
@@ -39,7 +39,10 @@
 import javax.management.MalformedObjectNameException;
 import javax.management.ObjectName;
 import javax.management.ReflectionException;
+import java.io.BufferedReader;
 import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Hashtable;
 
@@ -234,7 +237,8 @@
         try {
             ServerConfiguration config = ServerConfiguration.getInstance();
             CryptoUtil cryptoUtil =
-                    new CryptoUtil(new 
File(config.getFirstProperty("Security.KeyStore.Location")).getAbsolutePath(),
+                    new CryptoUtil(new File(config.getFirstProperty(
+                            "Security.KeyStore.Location")).getAbsolutePath(),
                                    
config.getFirstProperty("Security.KeyStore.Password"),
                                    
config.getFirstProperty("Security.KeyStore.KeyAlias"),
                                    
config.getFirstProperty("Security.KeyStore.KeyPassword"),
@@ -243,7 +247,117 @@
                     equals(password);
         } catch (Exception e) {
             log.error("Exception occurred during authenticating user " + 
userName, e);
-            throw new UserManagerException("Exception occurred during 
authenticating user " + userName);
+            throw new UserManagerException(
+                    "Exception occurred during authenticating user " + 
userName);
+        }
+    }
+
+    /**
+     * Reads a given JS service's source directly from the physical file. This 
ensures the availability
+     * of the service source even when the service is faulty and not deployed.
+     *
+     * @param path The path of the mashup
+     * @return serviceSource The source code contents of the service js file
+     */
+    public static String readServiceSource(String path) {
+        String serviceSource = "The specified mashup was not found on the 
server.";
+
+        //Extracting the real path from the registry path provided
+        String[] pathContents = path.split("/");
+        path = "";
+        for (int x = 2; x < pathContents.length; x++) {
+            path = path + "/" + pathContents[x];
+        }
+
+        ServerManager serverManager = ServerManager.getInstance();
+        ConfigurationContext configContext = serverManager.configContext;
+
+        File serviceJs = new 
File(configContext.getRealPath(configContext.getContextRoot()) +
+                "/scripts/" + path + ".js");
+
+        try {
+            if (serviceJs.exists()) {
+
+                StringBuffer fileData = new StringBuffer(1000);
+                BufferedReader reader = new BufferedReader(new 
FileReader(serviceJs));
+                char[] buf = new char[1024];
+                int numRead = 0;
+                while ((numRead = reader.read(buf)) != -1) {
+                    fileData.append(buf, 0, numRead);
+                }
+                reader.close();
+
+                serviceSource = fileData.toString();
+            }
+        } catch (IOException e) {
+            log.error("Failed to read mashup from disk.");
         }
+
+        return serviceSource;
+    }
+
+
+    /**
+     * Reads a given JS service's custom UI source directly from the physical 
file. This ensures the availability
+     * of the service UI source even when the service is faulty and not 
deployed.
+     *
+     * @param path The path of the mashup
+     * @return serviceSource The source code contents of the service js file
+     */
+    public static String readServiceUiSource(String path) {
+        String serviceUiSource =
+                "The custom UI code for the specified mashup was not found on 
the server.";
+
+        //Extracting the real path from the registry path provided
+        String[] pathContents = path.split("/");
+        path = "";
+        for (int x = 2; x < pathContents.length; x++) {
+            path = path + "/" + pathContents[x];
+        }
+
+        String serviceName = pathContents[pathContents.length - 1];
+
+        ServerManager serverManager = ServerManager.getInstance();
+        ConfigurationContext configContext = serverManager.configContext;
+
+        File serviceUiFile = new 
File(configContext.getRealPath(configContext.getContextRoot()) +
+                "/scripts/" + path + ".resources/www/index.html");
+
+        try {
+            if (serviceUiFile.exists()) {
+
+                StringBuffer fileData = new StringBuffer(1000);
+                BufferedReader reader = new BufferedReader(new 
FileReader(serviceUiFile));
+                char[] buf = new char[1024];
+                int numRead = 0;
+                while ((numRead = reader.read(buf)) != -1) {
+                    fileData.append(buf, 0, numRead);
+                }
+                reader.close();
+
+                serviceUiSource = fileData.toString();
+            } else {
+                serviceUiFile = new 
File(configContext.getRealPath(configContext.getContextRoot()) +
+                        "/scripts/" + path + ".resources/www/index.htm");
+
+                if (serviceUiFile.exists()) {
+
+                    StringBuffer fileData = new StringBuffer(1000);
+                    BufferedReader reader = new BufferedReader(new 
FileReader(serviceUiFile));
+                    char[] buf = new char[1024];
+                    int numRead = 0;
+                    while ((numRead = reader.read(buf)) != -1) {
+                        fileData.append(buf, 0, numRead);
+                    }
+                    reader.close();
+
+                    serviceUiSource = fileData.toString();
+                }
+            }
+        } catch (IOException e) {
+            log.error("Failed to read the mashup's custom ui from disk.");
+        }
+
+        return serviceUiSource;
     }
 }

Modified: trunk/mashup/java/modules/www/editor.jsp
==============================================================================
--- trunk/mashup/java/modules/www/editor.jsp    (original)
+++ trunk/mashup/java/modules/www/editor.jsp    Sat Jan 12 11:46:54 2008
@@ -15,11 +15,11 @@
 --%>
 <%@ page isErrorPage="true" %>
 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<%@ page import="org.wso2.mashup.utils.MashupUtils" %>
 <%@ page import="org.wso2.mashup.webapp.utils.RegistryUtils" %>
 <%@ page import="org.wso2.registry.Registry" %>
-<%@ page import="org.wso2.registry.RegistryConstants" %>
+<%@ page import="org.wso2.registry.Resource" %>
 <%@ page import="java.net.URLDecoder" %>
-<%@ page import="org.wso2.usermanager.UserManagerConstants" %>
 <%
 
     Registry registry = RegistryUtils.getRegistry(request);
@@ -43,16 +43,21 @@
     String action = request.getParameter("action");
     String mashup = request.getParameter("mashup");
 
+    String currentUser = RegistryUtils.getCurrentUser(registry);
+
     //Sanity check. Immediately terminate further processing if this fails.
     if (action == null) {
         throw new Exception("Sorry. An action was not specified in the 
request.");
     } else if ((action.equalsIgnoreCase("edit")) && (mashup == null)) {
         throw new Exception(
                 "Sorry. An edit request was made without a valid mashup being 
named for editing.");
-    } else if ((action.equalsIgnoreCase("edit")) &&
-            (!RegistryUtils.isAuthorized(registry, mashup, 
UserManagerConstants.EDIT))) {
-        throw new Exception(
-                "Sorry. You are not authorized to perform this operation.");
+    } else if ((action.equalsIgnoreCase("edit")) && (mashup != null)) {
+        Resource resource = registry.get(mashup);
+        String author = resource.getAuthorUserName();
+        if (!((author.equalsIgnoreCase(currentUser) || 
RegistryUtils.isAdminRole(registry)))) {
+            throw new Exception(
+                    "Sorry. You are not authorized to perform this 
operation.");
+        }
     } else if ((action.equalsIgnoreCase("new")) && (mashup == null)) {
         throw new Exception(
                 "Sorry. A request was made to create a mashup without 
providing a valid name.");
@@ -128,9 +133,16 @@
             return false;
         }
 
+        function init() {
+            try {
+                showPanel(document.getElementById('tab1'), 'mashup_code');
+            } catch(e) {
+            }
+        }
+
     </script>
 </head>
-<body onload="showPanel(document.getElementById('tab1'), 'mashup_code');">
+<body onload="init();">
 <div id="page">
 <%@ include file="header.jsp" %>
 <div id="search"></div>
@@ -160,56 +172,52 @@
        onclick="return false;">Custom UI Code</a>
 </div>
 <div class="panel" id="mashup_code" style="display: block">
-    <textarea id="mashup_code_text" class="codepress javascript"
-              style="width: 100%; height: 100%" cols="" rows="">
-        <%
-            if (action.equalsIgnoreCase("new")) {
-                //Inserting the initial mashup skeleton
-                String serviceSkeleton =
-                        "/*\n" +
-                                "* Copyright 2005-2007 WSO2, Inc. 
http://www.wso2.org\n"; +
-                                "*\n" +
-                                "* Licensed under the Apache License, Version 
2.0 (the \"License\");\n" +
-                                "* you may not use this file except in 
compliance with the License.\n" +
-                                "* You may obtain a copy of the License at\n" +
-                                "*\n" +
-                                "* 
http://www.apache.org/licenses/LICENSE-2.0\n"; +
-                                "*\n" +
-                                "* Unless required by applicable law or agreed 
to in writing, software\n" +
-                                "* distributed under the License is 
distributed on an \"AS IS\" BASIS,\n" +
-                                "* WITHOUT WARRANTIES OR CONDITIONS OF ANY 
KIND, either express or implied.\n" +
-                                "* See the License for the specific language 
governing permissions and\n" +
-                                "* limitations under the License.\n" +
-                                "*/\n" +
-                                "this.serviceName = \"" + mashup + "\";\n" +
-                                "this.documentation = \"TODO: Add service 
level documentation here\" ;\n" +
-                                "\n" +
-                                "toString.documentation = \"TODO: Add 
operation level documentation here\" ;\n" +
-                                "toString.inputTypes = { /* TODO: Add input 
types of this operation */ };\n" +
-                                "toString.outputType = \"String\"; /* TODO: 
Add output type here */ \n" +
-                                "function toString()\n" +
-                                "{\n" +
-                                "   //TODO: Add function code here\n" +
-                                "   return \"Hi, my name is " + mashup + 
"\";\n" +
-                                "}\n";
-
-        %>
-        <%=serviceSkeleton%>
-        <%
-        } else {
-        %>
-        <p>Existing code here</p>
-        <%
-            }
-        %>
+    <%
+        String serviceSource = "";
+        if (action.equalsIgnoreCase("new")) {
+            //Inserting the initial mashup skeleton
+            serviceSource = "/*\n" +
+                    "* Copyright 2005-2007 WSO2, Inc. http://www.wso2.org\n"; +
+                    "*\n" +
+                    "* Licensed under the Apache License, Version 2.0 (the 
\"License\");\n" +
+                    "* you may not use this file except in compliance with the 
License.\n" +
+                    "* You may obtain a copy of the License at\n" +
+                    "*\n" +
+                    "* http://www.apache.org/licenses/LICENSE-2.0\n"; +
+                    "*\n" +
+                    "* Unless required by applicable law or agreed to in 
writing, software\n" +
+                    "* distributed under the License is distributed on an \"AS 
IS\" BASIS,\n" +
+                    "* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 
express or implied.\n" +
+                    "* See the License for the specific language governing 
permissions and\n" +
+                    "* limitations under the License.\n" +
+                    "*/\n" +
+                    "this.serviceName = \"" + mashup + "\";\n" +
+                    "this.documentation = \"TODO: Add service level 
documentation here\" ;\n" +
+                    "\n" +
+                    "toString.documentation = \"TODO: Add operation level 
documentation here\" ;\n" +
+                    "toString.inputTypes = { /* TODO: Add input types of this 
operation */ };\n" +
+                    "toString.outputType = \"String\"; /* TODO: Add output 
type here */ \n" +
+                    "function toString()\n" +
+                    "{\n" +
+                    "   //TODO: Add function code here\n" +
+                    "   return \"Hi, my name is " + mashup + "\";\n" +
+                    "}\n";
+
+
+        } else if (action.equalsIgnoreCase("edit")) {
+            serviceSource = MashupUtils.readServiceSource(mashup);
+        }
+    %>
+    <textarea id="mashup_code_text" rows="" cols="" class="codepress 
javascript"
+              style="width: 100%; height: 100%;">
+        <%=serviceSource%>
     </textarea>
 </div>
 <div class="panel" id="ui_code" style="display: block">
-<textarea id="ui_code_text" rows="" cols="" class="codepress html"
-          style="width: 100%; height: 100%">
 <%
+    String uiSource = "";
     if (action.equalsIgnoreCase("new")) {
-        String uiSkeleton =
+        uiSource =
                 "<!--\n" +
                         "  ~ Copyright 2005-2007 WSO2, Inc. 
http://www.wso2.org\n"; +
                         "  ~\n" +
@@ -301,15 +309,12 @@
                         " \n" +
                         "</body>\n" +
                         "</html>";
-%>
-<%=uiSkeleton%>
-<%
-} else {
-%>
-<p>Exisiting ui code here</p>
-<%
+    } else if (action.equalsIgnoreCase("edit")) {
+        uiSource = MashupUtils.readServiceUiSource(mashup);
     }
 %>
+<textarea id="ui_code_text" rows="" cols="" class="codepress html"
+          style="width: 100%; height: 100%;"><%=uiSource%>
 </textarea>
 </div>
 </div>

_______________________________________________
Mashup-dev mailing list
[email protected]
http://www.wso2.org/cgi-bin/mailman/listinfo/mashup-dev

Reply via email to