KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > MountPointAction


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.frontend;
17
18 import java.util.Map JavaDoc;
19
20 import org.apache.avalon.framework.logger.LogEnabled;
21 import org.apache.avalon.framework.logger.Logger;
22 import org.apache.avalon.framework.parameters.Parameters;
23 import org.apache.avalon.framework.thread.ThreadSafe;
24 import org.apache.cocoon.acting.Action;
25 import org.apache.cocoon.environment.ObjectModelHelper;
26 import org.apache.cocoon.environment.Redirector;
27 import org.apache.cocoon.environment.Request;
28 import org.apache.cocoon.environment.SourceResolver;
29
30 import org.apache.cocoon.util.NetUtils;
31 import org.apache.excalibur.source.Source;
32
33 /**
34  * An action that tries to find out under which context the Cocoon servlet
35  * and current sitemap is mounted, and puts the result in a request attribute
36  * named "mountPoint". This assumes that the sitemap path is a substring
37  * of the external request URI, which is usually the case.
38  */

39 public class MountPointAction implements Action, ThreadSafe, LogEnabled {
40     private Logger logger;
41
42     public Map JavaDoc act(Redirector redirector, SourceResolver sourceResolver, Map JavaDoc objectModel, String JavaDoc source, Parameters parameters) throws Exception JavaDoc {
43         Request request = ObjectModelHelper.getRequest(objectModel);
44
45         if (request.getAttribute("mountPoint") == null) {
46             String JavaDoc mountPoint = "";
47             String JavaDoc requestURI = NetUtils.decodePath(request.getRequestURI());
48             // strip everything after ";", oherwise there might a problem when the
49
// URL contains eg a jsessionid and the sitemap URI is empty.
50
int semicolonPos = requestURI.lastIndexOf(';');
51             if (semicolonPos != -1)
52                 requestURI = requestURI.substring(0, semicolonPos);
53             String JavaDoc sitemapURI = request.getSitemapURI();
54             int pos = requestURI.lastIndexOf(sitemapURI);
55             if (pos != -1)
56                 mountPoint = requestURI.substring(0, pos - 1);
57             request.setAttribute("mountPoint", mountPoint);
58             if (getLogger().isDebugEnabled())
59                 getLogger().debug("mountPoint = " + mountPoint);
60
61             String JavaDoc contextPath = request.getContextPath();
62             String JavaDoc daisyCocoonPath;
63             if (contextPath.equals("")) {
64                 daisyCocoonPath = mountPoint;
65             } else {
66                 if (!mountPoint.startsWith(contextPath)) {
67                     throw new Exception JavaDoc("MountPoint does not start with contextPath.");
68                 }
69                 daisyCocoonPath = mountPoint.substring(contextPath.length());
70             }
71             request.setAttribute("daisyCocoonPath", daisyCocoonPath);
72
73
74             String JavaDoc daisyContextPath;
75             Source contextSource = sourceResolver.resolveURI("");
76             try {
77                 daisyContextPath = contextSource.getURI();
78             } finally {
79                 sourceResolver.release(contextSource);
80             }
81             request.setAttribute("daisyContextPath", daisyContextPath);
82         } else {
83             if (getLogger().isDebugEnabled())
84                 getLogger().debug("mountPoint = " + request.getAttribute("mountPoint") + " already set!");
85         }
86
87         return null;
88     }
89
90     public void enableLogging(Logger logger) {
91         this.logger = logger;
92     }
93
94     protected Logger getLogger() {
95         return this.logger;
96     }
97 }
98
Popular Tags