导航

用REST访问ALM的Servlet

Posted on 2013-07-17 21:09  zhustar  阅读(611)  评论(0编辑  收藏  举报

写了一个最简单的servlet用REST访问ALM。

需要用到ALM server端的两个包:

 org.hp.qc.web.restapi.docexamples.docexamples

org.hp.qc.web.restapi.docexamples.docexamples.infrastructure

 

Servlet的代码(待修改)如下:

public class AlmRestServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String serverUrl = request.getParameter("serverUrl");
        String port = request.getParameter("port");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String domain = request.getParameter("domain");
        String project = request.getParameter("project");
        String resource = request.getParameter("resource");

        String conUrl = "http://" + serverUrl + ":" + port + "/qcbin";

        RestConnector con = RestConnector.getInstance().init(
                new HashMap<String, String>(), conUrl, domain, project);

        // use the login example code to login for this test. go over this code
        // to learn how to authenticate/login/logout
        AuthenticateLoginLogoutExample login = new AuthenticateLoginLogoutExample();
        
        try {
            login.login(username, password);

            // read a simple resource, not even an entity..
            String urlOfResourceWeWantToRead = con.buildUrl(resource);

            Map<String, String> requestHeaders = new HashMap<String, String>();
            requestHeaders.put("Accept", "application/xml");

            Response serverResponse = con.httpGet(urlOfResourceWeWantToRead, null, requestHeaders);

            PrintWriter writer = response.getWriter();
            writer.write(serverResponse.toString());
        
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }          
    }
}

 

页面内容如下:通过form的action almrest来调用servlet。

<html>  
<head>  
<title>Server Information</title>  
</head>  
<body>  
    <form method="get" action="almrest">  
        <table>  
            <tr>  
                <td>host:</td>  
                <td><input type="text" name="serverUrl"></td>  
                <td>port:</td>  
                <td><input type="text" name="port"></td>
            </tr>
            <tr>  
                 
            </tr>
            <tr>  
                <td>username:</td>  
                <td><input type="text" name="username"></td>  
                <td>password:</td>  
                <td><input type="password" name="password"></td>  
            </tr>
            <tr>  
                
            </tr>            
            <tr>  
                <td>domain:</td>  
                <td><input type="text" name="domain"></td> 
                <td>project:</td>  
                <td><input type="text" name="project"></td>                  
            </tr>  
            <tr>  
                
            </tr> 
            <tr>  
                <td>resource:</td>  
                <td><input type="text" name="resource"></td>  
            </tr>            
            <tr>  
                <td><input type="submit" value="GetResource"></td>  
            </tr>  
        </table>  
    </form>  
</body>  
</html>

 

web.xml的配置如下:将/almrest配置成对servlet AlmRestServlet的调用。

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">

  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
  
  <servlet>
        <servlet-name>AlmRestServlet</servlet-name>
        <servlet-class>stan.AlmRestServlet</servlet-class>
  </servlet>
    
  <servlet-mapping>
        <servlet-name>AlmRestServlet</servlet-name>
        <url-pattern>/almrest</url-pattern>
  </servlet-mapping>


</web-app>

将java文件build后的class文件按照包的层级关系考到WEB-INF的classes文件夹下。

运行的结果如下:

点击 GetResource之后的效果: