1 /*
2 * Copyright (c) 2005-2007 Creative Sphere Limited.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *
10 * Creative Sphere - initial API and implementation
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 * This class selects appropriate {@link org.abstracthorizon.danube.connection.ConnectionHandler}
26 * based on matching based on the URI from the http connection.
27 *
28 * @author Daniel Sendula
29 *
30 * @has 1 - n org.abstracthorizon.danube.http.matcher.Matcher
31 */
32 public class Selector implements ConnectionHandler {
33
34 /** List of {@link org.abstracthorizon.danube.http.matcher.Matcher} objects */
35 protected List<Matcher> components = new ArrayList<Matcher>();
36
37 /** Error response if uri hasn't been matched */
38 protected ConnectionHandler errorResponse = new ErrorConnectionHandler();
39
40 /** Constructor */
41 public Selector() {
42 }
43
44 /**
45 * <p>Selects {@link ConnectionHandler} checking all {@link Matcher} from {@link #components} list.
46 * If {@link Matcher#matches(HTTPConnection)} returns true then {@link ConnectionHandler} stored in
47 * {@link Matcher} is called. Depending on {@link Matcher#isStopOnMatch()} selector will
48 * proceed with rest of the list or not.
49 * </p>
50 * <p>If no matches are found {@link #errorResponse} is used to generate error. Default
51 * value is {@link ErrorConnectionHandler}.
52 *
53 * @param connection http connection
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 * Sets list of connection handlers
80 * @param components list of connection handlers
81 */
82 public void setComponents(List<Matcher> components) {
83 this.components = components;
84 }
85
86 /**
87 * Returns list of connection handlers
88 * @return list of connection handlers
89 */
90 public List<Matcher> getComponents() {
91 return components;
92 }
93
94 /**
95 * Returns error response
96 * @return error response
97 */
98 public ConnectionHandler getErrorResponse() {
99 return errorResponse;
100 }
101
102 /**
103 * Sets error response
104 * @param errorResponse error response
105 */
106 public void setErrorResponse(ConnectionHandler errorResponse) {
107 this.errorResponse = errorResponse;
108 }
109
110 }