1 package com.atlassian.plugin.servlet;
2
3 import com.atlassian.plugin.servlet.descriptors.BaseServletModuleDescriptor;
4 import org.apache.commons.lang.StringUtils;
5
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletRequestWrapper;
8 import javax.servlet.http.HttpSession;
9
10
11
12
13
14
15
16 public class PluginHttpRequestWrapper extends HttpServletRequestWrapper {
17 private final String basePath;
18 private HttpServletRequest delegate;
19
20 public PluginHttpRequestWrapper(HttpServletRequest request, BaseServletModuleDescriptor<?> descriptor) {
21 super(request);
22 this.delegate = request;
23 this.basePath = findBasePath(descriptor);
24 }
25
26 public String getServletPath() {
27 String servletPath = super.getServletPath();
28 if (basePath != null) {
29 servletPath += basePath;
30 }
31 return servletPath;
32 }
33
34 public String getPathInfo() {
35 String pathInfo = super.getPathInfo();
36 if (pathInfo != null && basePath != null) {
37 if (basePath.equals(pathInfo)) {
38 return null;
39 } else if (pathInfo.startsWith(basePath)) {
40 return pathInfo.substring(basePath.length());
41 }
42 }
43 return pathInfo;
44 }
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 private String findBasePath(BaseServletModuleDescriptor<?> descriptor) {
65 String pathInfo = super.getPathInfo();
66
67 if (pathInfo != null) {
68
69 for (String basePath : descriptor.getPaths()) {
70 if (basePath.equals(pathInfo)) {
71 return basePath;
72 }
73 }
74
75
76 final String[] pathInfoComponents = StringUtils.split(pathInfo, '/');
77 for (String basePath : descriptor.getPaths()) {
78 if (isPathMapping(basePath)) {
79 final String mappingRootPath = getMappingRootPath(basePath);
80 final String[] mappingRootPathComponents = StringUtils.split(mappingRootPath, '/');
81
82 if (arrayStartsWith(pathInfoComponents, mappingRootPathComponents)) {
83 return mappingRootPath;
84 }
85 }
86 }
87 }
88 return null;
89 }
90
91 private static boolean arrayStartsWith(String[] array, String[] prefixArray) {
92
93 if (prefixArray.length > array.length) {
94 return false;
95 }
96
97
98 for (int i = prefixArray.length - 1; i >= 0; i--) {
99 if (!prefixArray[i].equals(array[i])) {
100 return false;
101 }
102 }
103
104 return true;
105 }
106
107 private boolean isPathMapping(String path) {
108 return path.startsWith("/") && path.endsWith("/*");
109 }
110
111 private String getMappingRootPath(String pathMapping) {
112 return pathMapping.substring(0, pathMapping.length() - "/*".length());
113 }
114
115 @Override
116 public HttpSession getSession() {
117 return this.getSession(true);
118 }
119
120 @Override
121 public HttpSession getSession(final boolean create) {
122 HttpSession session = delegate.getSession(create);
123 if (session == null) {
124
125 return null;
126 } else {
127
128 return new PluginHttpSessionWrapper(session);
129 }
130 }
131 }