IT-KIMI_SHI\SINOIT.KIMI
2018-06-01 521993214708c66a5498bd669c94a661c11484b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package frame.object.http;
 
import java.net.URLDecoder;
import java.util.HashSet;
import java.util.Set;
 
import javax.servlet.http.HttpServletRequest;
 
import frame.data.DataType;
 
public class Path {
    
    protected String pathString;
    protected String[] pathArray;
    private String objectName;
    private String operator;
    private String dataName;
    private DataType returnType;
    private boolean valid;
 
    private static Set<String> pageTypes;
    public static int contextLength;
    private HttpServletRequest request;
    private String uri;
    private String target;  //去掉项目名,去掉root标签,并以/开头
    private String shortTarget;  //去掉项目名,去掉root标签,不以/开头
    private String suffix;  //请求后缀,没用则为VirtualPath路径,有则为Resource路径
    private String leaf;  ////target的最后一截
    private String parent;  //target的第一截
    private RequestType type;  //VirtualPath、Resource
 
    static {
        pageTypes = new HashSet<String>();
        pageTypes.add(".html");
        pageTypes.add(".htm");
        pageTypes.add(".jsp");
    }
 
    
    public Path(HttpServletRequest request) {
        this.request = request;
 
        // /sfez/xxx/xxx/xx.html,uri是不带请求参数的,即不会带?xxx=xxx
        uri = request.getRequestURI();
        
        try {
            uri = URLDecoder.decode(uri, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        // 1. delete parameters(没用)
        int last = uri.length();
        int pos_param = uri.indexOf("?");
        if (pos_param > 0) {
            last = pos_param;
        }
 
        
        target = uri.substring(contextLength, last);
 
        // 2. root
        if ("/".equals(target)) {
            type = RequestType.VirtualPath;
            return;
        }
 
        // 3. find root
        int pos = target.lastIndexOf("/root/");
        if (pos >= 0) {
            target = target.substring(pos + "/root/".length());
        }
 
        if (target.charAt(0) != '/') {
            target = "/" + target;
        }
 
        // 4. resource
        int pos_dot = target.lastIndexOf(".");
 
        if (pos_dot > 0) {
            suffix = target.substring(pos_dot);
            type = RequestType.Resource;
        } else {
            type = RequestType.VirtualPath;
        }
 
        int pos_begin = target.indexOf("/", 1);
        if (pos_begin > 0) {
            parent = target.substring(0, pos_begin);
        }
 
        int pos_end = target.lastIndexOf("/");
        if (pos_end >= pos_begin) {
            leaf = target.substring(pos_end);
        }
 
        shortTarget = target.substring(1);
    }
 
    public String getSuffix() {
        return suffix;
    }
 
    public String getTarget() {
        return target;
    }
 
    public String getShortTarget() {
        return shortTarget;
    }
 
    public String getLeaf() {
        return leaf;
    }
 
    public String getParent() {
        return parent;
    }
 
    public HttpServletRequest getRequest() {
        return request;
    }
 
    public String getURI() {
        return uri;
    }
 
    public RequestType getType() {
        return type;
    }
 
    @Override
    public String toString() {
        return uri;
    }
    
    public Path(String pathString) {
        this.pathString = pathString;
        String[] paths = pathString.split("/");
        int length = paths.length;
        
        pathArray = paths;
        valid = length >= 2;
        
        if (valid) {
            objectName = paths[0];
            operator = paths[1];
            
            if (length > 2) {
                dataName = paths[2];
                
                if (length > 3) {
                    returnType = DataType.valueOfString(paths[3]);
                }
            }
        }
    }
 
    public String getObjectName() {
        return objectName;
    }
 
    public String getOperator() {
        return operator;
    }
 
    public String getDataName() {
        return dataName;
    }
 
    public String[] getPathArray() {
        return pathArray;
    }
    
    public DataType getReturnType() {
        return returnType;
    }
    
    public String getPathString() {
        return pathString;
    }
 
    public boolean isValid() {
        return valid;
    }
 
}