1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.danube.http;
14
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
18
19 import org.abstracthorizon.danube.connection.Connection;
20 import org.abstracthorizon.danube.connection.ConnectionHandler;
21 import org.abstracthorizon.danube.http.matcher.Matcher;
22 import org.abstracthorizon.danube.http.util.ErrorConnectionHandler;
23
24
25
26
27
28
29
30
31
32 public class Selector implements ConnectionHandler {
33
34
35 protected List<Matcher> components = new ArrayList<Matcher>();
36
37
38 protected ConnectionHandler errorResponse = new ErrorConnectionHandler();
39
40
41 public Selector() {
42 }
43
44
45
46
47
48
49
50
51
52
53
54
55 public void handleConnection(Connection connection) {
56 HTTPConnection httpConnection = (HTTPConnection)connection.adapt(HTTPConnection.class);
57
58 boolean matched = false;
59 Iterator<Matcher> it = components.iterator();
60 while (it.hasNext()) {
61 Matcher httpMatcher = it.next();
62 if (httpMatcher.matches(httpConnection)) {
63 matched = true;
64 httpMatcher.adjustForInvocation(httpConnection);
65 httpMatcher.getConnectionHandler().handleConnection(httpConnection);
66 if (httpMatcher.isStopOnMatch()) {
67 return;
68 }
69 }
70 }
71
72 if (!matched) {
73 httpConnection.setResponseStatus(Status.NOT_FOUND);
74 errorResponse.handleConnection(httpConnection);
75 }
76 }
77
78
79
80
81
82 public void setComponents(List<Matcher> components) {
83 this.components = components;
84 }
85
86
87
88
89
90 public List<Matcher> getComponents() {
91 return components;
92 }
93
94
95
96
97
98 public ConnectionHandler getErrorResponse() {
99 return errorResponse;
100 }
101
102
103
104
105
106 public void setErrorResponse(ConnectionHandler errorResponse) {
107 this.errorResponse = errorResponse;
108 }
109
110 }