[Registry-dev] svn commit r11777 - in trunk/registry/modules:
core/src/main/java/org/wso2/registry
core/src/main/java/org/wso2/registry/servlet webapps/conf
svn at wso2.org
svn at wso2.org
Wed Jan 2 20:39:20 PST 2008
Author: chathura
Date: Wed Jan 2 20:39:08 2008
New Revision: 11777
Log:
Moved the handling of /resources path to the core module. This is not specific to the UI as the APP interface also uses this for retrieving resource content.
Now we can add caching support and basic auth support to this path.
Added:
trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/ResourceHandlerUtil.java
Modified:
trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java
trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
trunk/registry/modules/webapps/conf/web.xml
Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java (original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/RegistryConstants.java Wed Jan 2 20:39:08 2008
@@ -134,6 +134,8 @@
public static final String RESOURCES_JSP = "/admin/registry-resources.jsp";
public static final String RESOURCE_DETAILS_JSP = "/admin/resources_details.jsp";
+ public static final String ERROR_MESSAGE = "error.message";
+ public static final String ERROR_JSP = "/admin/error.jsp";
public static final String PATH_ATTR = "path";
// Custom elements in Atom feeds and entries
@@ -142,4 +144,6 @@
// Separator used to access Registry metadata - i.e. "/resource$tags"
public static final String URL_SEPARATOR = "$";
+
+ public static final String RESOURCES_PATH = "resources";
}
Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java (original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java Wed Jan 2 20:39:08 2008
@@ -84,7 +84,7 @@
} else {
DataSource dataSource;
if (dbConfiguration.getDataSourceName() != null &&
- dbConfiguration.getDataSourceName().length() > 0) {
+ dbConfiguration.getDataSourceName().length() > 0) {
try {
Context context = new InitialContext();
dataSource =
@@ -93,8 +93,8 @@
} catch (NamingException e) {
String msg = "Could not find the specified data source named: " +
- dbConfiguration.getDataSourceName() + ". Caused by: " +
- e.getMessage();
+ dbConfiguration.getDataSourceName() + ". Caused by: " +
+ e.getMessage();
log.fatal(msg, e);
throw new ServletException(msg);
}
@@ -113,9 +113,9 @@
// create a system registry and put it in the context
SecureRegistry systemRegistry =
new SecureRegistry(RegistryConstants.SYSTEM_USER,
- RegistryConstants.SYSTEM_PASSWORD,
- coreRegistry,
- registryRealm);
+ RegistryConstants.SYSTEM_PASSWORD,
+ coreRegistry,
+ registryRealm);
// todo: we should make this decision according to the registry.xml or web.xml config
//try {
@@ -165,8 +165,8 @@
String path = fileElement.getPath();
registry.put(path, fileElement);
request.getSession().setAttribute(RegistryConstants.STATUS_MESSAGE_NAME,
- "Resource " + path +
- " is successfully added to the registry.");
+ "Resource " + path +
+ " is successfully added to the registry.");
response.setContentType("text/html");
request.getRequestDispatcher(RegistryConstants.RESOURCES_JSP)
.forward(request, response);
@@ -175,7 +175,7 @@
e.printStackTrace();
request.getSession().setAttribute(RegistryConstants.STATUS_MESSAGE_NAME,
- e.getMessage());
+ e.getMessage());
response.setContentType("text/html");
request.getRequestDispatcher(RegistryConstants.RESOURCES_JSP)
.forward(request, response);
@@ -186,6 +186,55 @@
HttpServletResponse response)
throws ServletException, IOException {
initContextRoot(request);
+
+ String uri = request.getRequestURI();
+ String controlPart = uri.substring(contextRoot.length());
+
+ if (controlPart.startsWith(
+ RegistryConstants.PATH_SEPARATOR + RegistryConstants.RESOURCES_PATH)) {
+
+ String path = null;
+
+ if (uri.equals("") || uri.endsWith(RegistryConstants.RESOURCES_PATH +
+ RegistryConstants.PATH_SEPARATOR)) {
+
+ path = RegistryConstants.ROOT_PATH;
+
+ } else {
+ path = uri.substring((contextRoot + RegistryConstants.PATH_SEPARATOR +
+ RegistryConstants.RESOURCES_PATH).
+
+ length(), uri.length());
+ }
+
+ // if user is browsing an old version of the resource, we append it to the path, so that
+ // the backend registry gives the details of the version
+ String qPart = request.getQueryString();
+ if (qPart != null && qPart.startsWith("v")) {
+ path = path + "?" + qPart;
+ }
+
+ Resource resource = null;
+ try {
+ resource = ResourceHandlerUtil.getResource(request, path);
+ } catch (RegistryException e) {
+ request.getSession().setAttribute(RegistryConstants.ERROR_MESSAGE, e.getMessage());
+ request.getRequestDispatcher(RegistryConstants.ERROR_JSP);
+ return;
+ }
+
+ if (resource == null) {
+ request.getSession().setAttribute(RegistryConstants.ERROR_MESSAGE, "404 Not Found");
+ request.getRequestDispatcher(RegistryConstants.ERROR_JSP);
+ return;
+ }
+
+ if (resource.isDirectory()) {
+ response.sendRedirect("/wso2registry/web" + path);
+ } else {
+ sendResourceContent(request, response, path);
+ }
+ }
}
/**
@@ -231,4 +280,42 @@
//
}
}
+
+ private void sendResourceContent(HttpServletRequest request, HttpServletResponse response, String path) {
+
+ Resource resource = null;
+ try {
+ resource = ResourceHandlerUtil.getResource(request, path);
+ } catch (RegistryException e) {
+ setErrorMessage(request, e.getMessage());
+ e.printStackTrace();
+ }
+
+ try {
+ Object content = resource.getContent();
+ if (content != null) {
+
+ if (resource.getMediaType() != null && resource.getMediaType().length() > 0) {
+ response.setContentType(resource.getMediaType());
+ } else {
+ response.setContentType("application/download");
+ }
+
+ if (content instanceof byte[]) {
+ response.getOutputStream().write((byte[]) content);
+ response.flushBuffer();
+ } else {
+ response.getWriter().write(content.toString());
+ }
+ }
+
+ response.getWriter().flush();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void setErrorMessage(HttpServletRequest request, String message) {
+ request.getSession().setAttribute(RegistryConstants.ERROR_MESSAGE, message);
+ }
}
Added: trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/ResourceHandlerUtil.java
==============================================================================
--- (empty file)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/ResourceHandlerUtil.java Wed Jan 2 20:39:08 2008
@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2006, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.wso2.registry.servlet;
+
+import org.wso2.registry.Resource;
+import org.wso2.registry.RegistryException;
+import org.wso2.registry.Registry;
+import org.wso2.registry.RegistryConstants;
+import org.wso2.registry.jdbc.realm.RegistryRealm;
+import org.wso2.registry.secure.SecureRegistry;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.ServletContext;
+
+public class ResourceHandlerUtil {
+
+ public static Resource getResource(HttpServletRequest request, String path)
+ throws RegistryException {
+
+ SecureRegistry secureRegistry = getUserRegistry(request);
+
+ String resourcePath = path;
+ if (path.indexOf("?") > 0) {
+ resourcePath = path.substring(0, path.indexOf("?"));
+ }
+
+ if (secureRegistry.resourceExists(resourcePath)) {
+ return secureRegistry.get(path);
+ } else {
+ return null;
+ }
+ }
+
+ public static SecureRegistry getUserRegistry(HttpServletRequest request)
+ throws RegistryException {
+
+ SecureRegistry userRegistry =
+ (SecureRegistry) request.getSession().getAttribute(RegistryConstants.USER_REGISTRY);
+
+ if (userRegistry == null) {
+
+ // user is not logged in. create a annoymous userRegistry for the user.
+
+ ServletContext context =
+ request.getSession().getServletContext();
+
+ Registry jdbcRegistry =
+ (Registry) context.getAttribute(RegistryConstants.REGISTRY);
+
+
+ RegistryRealm realm = (RegistryRealm) request.getSession().getServletContext().
+ getAttribute(RegistryConstants.REGISTRY_REALM);
+
+ SecureRegistry secureRegistry = new SecureRegistry(
+ RegistryConstants.ANONYMOUS_USER, RegistryConstants.ANONYMOUS_PASSWORD, jdbcRegistry, realm);
+
+ request.getSession().setAttribute(RegistryConstants.USER_REGISTRY, secureRegistry);
+
+ userRegistry = secureRegistry;
+ }
+
+ return userRegistry;
+ }
+}
Modified: trunk/registry/modules/webapps/conf/web.xml
==============================================================================
--- trunk/registry/modules/webapps/conf/web.xml (original)
+++ trunk/registry/modules/webapps/conf/web.xml Wed Jan 2 20:39:08 2008
@@ -40,7 +40,7 @@
<url-pattern>/system/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
- <servlet-name>WebServlet</servlet-name>
+ <servlet-name>RegistryServlet</servlet-name>
<url-pattern>/resources/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
More information about the Registry-dev
mailing list