summaryrefslogtreecommitdiffstats
path: root/sandbox/lresende/sca/samples/expertise-restfull/src/main/java/org/apache/tuscany/expertise/restfull/RestfulExpertiseLocator.java
blob: ea3915ccb582e93d1f7fce700a011b7350ce45d7 (plain)
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */

package org.apache.tuscany.expertise.restfull;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tuscany.expertise.Expert;
import org.osoa.sca.annotations.Reference;
import org.osoa.sca.annotations.Scope;
import org.osoa.sca.annotations.Service;

@Service(Servlet.class)
@Scope("COMPOSITE")
public class RestfulExpertiseLocator  extends HttpServlet implements Servlet {

	private static final long serialVersionUID = 4021701701436888369L;
	
	@Reference
	protected ExpertRegistry experts;
	

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path = URLDecoder.decode(getRequestPath(request), "UTF-8");
		String expertise = path.substring(1);
		
		if (expertise != null && expertise.length() > 0 ) {
			printExperts(response.getOutputStream(), experts.getExpertsByExpertise(expertise));
		} else {
			printExperts(response.getOutputStream(), experts.getExperts());
		}
		
	}
	
	
	private void printExperts(ServletOutputStream out, List<Expert> experts) throws IOException {
		out.print("<html><body>");
		out.print("<h2>Expertise Locator</h2>");
		
		if (experts.size() > 0) {
			for (Expert expert: experts) {
				out.print("<b>Location:</b> " + expert.getLocation() + " - "  + expert.getName() +" ==> ");
				for (String expertise : expert.getExpertise()) {
					out.print(expertise + ";");
				}
				out.print("<br/>");
			}			
		} else {
			out.println("No experts found");
		}
		out.print("</body></html>");
	}
	
	  /**
     * 
     * @param request
     * @return
     */
    private static String getRequestPath(HttpServletRequest request) {
        // Get the request path
        String contextPath = request.getContextPath();
        String servletPath = request.getServletPath();
        String requestURI = request.getRequestURI();
        
        int contextPathLength = request.getContextPath().length();
        int servletPathLenght = servletPath.contains(contextPath) ? servletPath.length() - contextPath.length() : servletPath.length();
        
        String requestPath = requestURI.substring(contextPathLength + servletPathLenght);
        
        return requestPath;
    }
  

}