WsdmWsdlPerspectiveAction.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.management.web.action;
022 
023 import java.io.IOException;
024 import java.net.URI;
025 
026 import javax.management.ObjectName;
027 import javax.servlet.RequestDispatcher;
028 import javax.servlet.ServletException;
029 import javax.servlet.http.HttpServlet;
030 import javax.servlet.http.HttpServletRequest;
031 import javax.servlet.http.HttpServletResponse;
032 import javax.xml.namespace.QName;
033 
034 import org.apache.muse.core.proxy.ProxyHandler;
035 import org.apache.muse.core.proxy.ReflectionProxyHandler;
036 import org.apache.muse.util.xml.XmlUtils;
037 import org.apache.muse.ws.addressing.EndpointReference;
038 import org.apache.muse.ws.resource.remote.WsResourceClient;
039 import org.apache.qpid.management.Names;
040 import org.w3c.dom.Element;
041 import org.w3c.dom.Node;
042 import org.w3c.dom.NodeList;
043 
044 public class WsdmWsdlPerspectiveAction extends HttpServlet
045 {
046   private static final long serialVersionUID = -2411413147821629363L;
047   private static final Object [] WSDL_DIALECT = new Object[]{"http://schemas.xmlsoap.org/wsdl/"};
048   
049   private ProxyHandler proxyHandler;
050   
051   private URI resourceUri;
052     
053   @Override
054   public void init() throws ServletException
055   {
056     proxyHandler  = new ReflectionProxyHandler();
057     proxyHandler.setAction("http://schemas.xmlsoap.org/ws/2004/09/mex/GetMetadata");
058     proxyHandler.setRequestName(new QName("http://schemas.xmlsoap.org/ws/2004/09/mex""GetMetadata", Names.PREFIX));
059     proxyHandler.setRequestParameterNames(new QName[]{new QName("http://schemas.xmlsoap.org/ws/2004/09/mex""Dialect", Names.PREFIX)});
060     proxyHandler.setResponseName(new QName("http://schemas.xmlsoap.org/ws/2004/09/mex""Metadata", Names.PREFIX));
061     proxyHandler.setReturnType(Element[].class);
062   }
063   
064   @SuppressWarnings("unchecked")
065   @Override
066   protected void service(HttpServletRequest request, HttpServletResponse responsethrows ServletException, IOException
067   {
068     try 
069     {
070       String resourceId = request.getParameter("resourceId");
071       ObjectName objectName = new ObjectName(resourceId);
072 
073       String wsresourceid = objectName.getKeyProperty(Names.OBJECT_ID);
074       EndpointReference resourceEndpointReference = new EndpointReference(getURI(request));
075       resourceEndpointReference.addParameter(
076           Names.RESOURCE_ID_QNAME, 
077           wsresourceid);
078       
079       WsResourceClient resourceClient = new WsResourceClient(resourceEndpointReference);
080       Element wsdl = ((Element[])resourceClient.invoke(proxyHandler,WSDL_DIALECT))[0];
081             
082           NodeList nodelist = wsdl.getChildNodes();
083           Element definitions = null;
084           for (int i = 0; i < nodelist.getLength(); i++)
085           {
086             Node node = nodelist.item(i);
087             switch (node.getNodeType())
088             {
089               case Node.ELEMENT_NODE:
090               {
091                 Element element = (Elementnode;
092                 if (element.getNodeName().indexOf("definitions"!= -1)
093                 {
094                   definitions = element;
095                   break;
096                 }
097               }
098             }
099           }
100             
101           String output = XmlUtils.toString(definitions);
102           
103       String [] keyProperties = objectName.getKeyPropertyListString().split(",");
104       
105       request.setAttribute("resourceId", resourceId);
106       request.setAttribute("nameAttributes",keyProperties);
107       request.setAttribute("wsdl",output);
108       RequestDispatcher dispatcher = request.getRequestDispatcher("/wsdm_wsdl_perspective.jsp");
109       dispatcher.forward(request,response)
110     catch(Exception exception)
111     {      
112       request.setAttribute("errorMessage","Unable to detect the exact cause Please look at the reported stack trace below.");
113       request.setAttribute("exception",exception);
114       RequestDispatcher dispatcher = request.getRequestDispatcher("/error_page.jsp");
115       dispatcher.forward(request,response);      
116     }
117   }
118 
119   private URI getURI(HttpServletRequest request)
120   {
121     if (resourceUri == null)
122     {
123       StringBuilder builder = new StringBuilder();
124       builder
125         .append("http://")
126         .append(request.getServerName())
127         .append(":")
128         .append(request.getServerPort())
129         .append("/qman/services/QManWsResource");
130       resourceUri = URI.create(builder.toString());
131     }
132     return resourceUri;
133   }
134 }