diff options
Diffstat (limited to 'sandbox/lresende/sca-1.x/samples/expertise-restfull/src/main/java/org/apache/tuscany/expertise/restfull/RestfulExpertiseLocator.java')
-rw-r--r-- | sandbox/lresende/sca-1.x/samples/expertise-restfull/src/main/java/org/apache/tuscany/expertise/restfull/RestfulExpertiseLocator.java | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/sandbox/lresende/sca-1.x/samples/expertise-restfull/src/main/java/org/apache/tuscany/expertise/restfull/RestfulExpertiseLocator.java b/sandbox/lresende/sca-1.x/samples/expertise-restfull/src/main/java/org/apache/tuscany/expertise/restfull/RestfulExpertiseLocator.java new file mode 100644 index 0000000000..ea3915ccb5 --- /dev/null +++ b/sandbox/lresende/sca-1.x/samples/expertise-restfull/src/main/java/org/apache/tuscany/expertise/restfull/RestfulExpertiseLocator.java @@ -0,0 +1,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; + } + + +} |