2
* Copyright 2005-2012 Restlet S.A.S.
4
* The contents of this file are subject to the terms of one of the following
5
* open source licenses: Apache 2.0 or LGPL 3.0 or LGPL 2.1 or CDDL 1.0 or EPL
6
* 1.0 (the "Licenses"). You can select the license that you prefer but you may
7
* not use this file except in compliance with one of these Licenses.
9
* You can obtain a copy of the Apache 2.0 license at
10
* http://www.opensource.org/licenses/apache-2.0
12
* You can obtain a copy of the LGPL 3.0 license at
13
* http://www.opensource.org/licenses/lgpl-3.0
15
* You can obtain a copy of the LGPL 2.1 license at
16
* http://www.opensource.org/licenses/lgpl-2.1
18
* You can obtain a copy of the CDDL 1.0 license at
19
* http://www.opensource.org/licenses/cddl1
21
* You can obtain a copy of the EPL 1.0 license at
22
* http://www.opensource.org/licenses/eclipse-1.0
24
* See the Licenses for the specific language governing permissions and
25
* limitations under the Licenses.
27
* Alternatively, you can obtain a royalty free commercial license with less
28
* limitations, transferable or non-transferable, directly at
29
* http://www.restlet.com/products/restlet-framework
31
* Restlet is a registered trademark of Restlet S.A.S.
34
package org.restlet.example.book.restlet.ch07.sec3.sub2;
36
import org.restlet.Application;
37
import org.restlet.Component;
38
import org.restlet.Restlet;
39
import org.restlet.data.Protocol;
40
import org.restlet.routing.Extractor;
41
import org.restlet.routing.Redirector;
42
import org.restlet.routing.Router;
45
* URI rewriting, attribute extraction and redirection.
47
* @author Jerome Louvel
49
public class SearchRedirector extends Application {
51
public static void main(String[] args) throws Exception {
53
Component component = new Component();
54
component.getServers().add(Protocol.HTTP, 8111);
56
// Create an application
57
Application application = new SearchRedirector();
59
// Attach the application to the component and start it
60
component.getDefaultHost().attachDefault(application);
65
public Restlet createInboundRoot() {
66
// Create a root router
67
Router router = new Router(getContext());
69
// Create a Redirector to Google search service
70
String target = "http://www.google.com/search?q=site:mysite.org+{keywords}";
71
Redirector redirector = new Redirector(getContext(), target,
72
Redirector.MODE_CLIENT_TEMPORARY);
74
// While routing requests to the redirector, extract the "kwd" query
75
// parameter. For instance :
76
// http://localhost:8111/search?kwd=myKeyword1+myKeyword2
78
// http://www.google.com/search?q=site:mysite.org+myKeyword1%20myKeyword2
79
Extractor extractor = new Extractor(getContext(), redirector);
80
extractor.extractFromQuery("keywords", "kwd", true);
82
// Attach the extractor to the router
83
router.attach("/search", extractor);
85
// Return the root router