01: /**
02: * Copyright 2006 Webmedia Group Ltd.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: **/package org.araneaframework.http.router;
16:
17: import java.util.HashMap;
18: import java.util.Map;
19: import org.araneaframework.Environment;
20: import org.araneaframework.InputData;
21: import org.araneaframework.OutputData;
22: import org.araneaframework.Path;
23: import org.araneaframework.core.StandardEnvironment;
24: import org.araneaframework.framework.router.BaseServiceRouterService;
25: import org.araneaframework.http.HttpInputData;
26: import org.araneaframework.http.PathInfoServiceContext;
27:
28: /**
29: * TODO: document it
30: * @author "Jevgeni Kabanov" <ekabanov@webmedia.ee>
31: */
32: public class PathInfoServiceRouterService extends
33: BaseServiceRouterService {
34:
35: private String pathInfo;
36:
37: protected Object getServiceIdFromInput(InputData input)
38: throws Exception {
39: return getPathInfo(input)[0];
40: }
41:
42: protected Object getServiceKey() throws Exception {
43: return "pathInfoServiceId";
44: }
45:
46: protected void action(Path path, InputData input, OutputData output)
47: throws Exception {
48: pathInfo = getPathInfo(input)[1];
49: super .action(path, input, output);
50: }
51:
52: private static String[] getPathInfo(InputData input) {
53: String serviceId = null;
54: String pathInfo = "";
55:
56: String path = ((HttpInputData) input).getPath();
57: if (path != null) {
58: // lose the first slashes
59: while (path.indexOf("/") == 0 && path.length() > 0)
60: path = path.substring(1);
61:
62: int index = path.indexOf("/");
63: // we have a second slash
64: if (index != -1) {
65: // not interested in the first slash
66: pathInfo = path.substring(index + 1);
67: serviceId = path.substring(0, index);
68: } else {
69: serviceId = path;
70: }
71: }
72: serviceId = serviceId != null && serviceId.length() == 0 ? null
73: : serviceId;
74:
75: return new String[] { serviceId, pathInfo };
76: }
77:
78: protected Environment getChildEnvironment(Object serviceId)
79: throws Exception {
80: Map entries = new HashMap();
81: entries.put(PathInfoServiceContext.class,
82: new ServiceRouterContextImpl(serviceId));
83: return new StandardEnvironment(super
84: .getChildEnvironment(serviceId), entries);
85: }
86:
87: private class ServiceRouterContextImpl extends
88: BaseServiceRouterService.ServiceRouterContextImpl implements
89: PathInfoServiceContext {
90: protected ServiceRouterContextImpl(Object serviceId) {
91: super (serviceId);
92: }
93:
94: public String getPathInfo() {
95: return pathInfo;
96: }
97: }
98: }
|