java-xwiki restful接口简单测试

xwiki测试流程

  1. 测试页面
    public void normalTest()throws Exception, IOException{
        CloseableHttpClient httpclient = HttpClients.createDefault();
        JAXBContext context = JAXBContext.newInstance("org.xwiki.rest.model.jaxb");
        Unmarshaller unmarshaller = context.createUnmarshaller();
        HttpUriRequest httpPost = RequestBuilder.get()
                .setUri(new URI("http://172.16.200.220:8082/xwiki/rest/wikis/xwiki/spaces/Main/pages/WebHome"))
                .setHeader("Accept", "application/xml").build();
        CloseableHttpResponse response = httpclient.execute(httpPost);
        System.out.println(response.getEntity());
        HttpEntity responseEntity=response.getEntity();

        Page page = (Page) unmarshaller.unmarshal(responseEntity.getContent());
        System.out.println(new Gson().toJson(page));
    }
  1. 登录并获取session
    public void testLogin() throws Exception{
        CloseableHttpClient httpclient = HttpClients.createDefault();
        JAXBContext context = JAXBContext.newInstance("org.xwiki.rest.model.jaxb");
        HttpUriRequest httpPost = RequestBuilder.post()
                .setUri(new URI("http://172.16.200.220:8082/xwiki/bin/loginsubmit/XWiki/XWikiLogin"))
                .addParameter("j_username", "superadmin")
                .addParameter("j_password", "system")
                .setHeader("Accept", "application/xml").build();
        CloseableHttpResponse response = httpclient.execute(httpPost);
        System.out.println(response.getStatusLine().getStatusCode());
        HttpEntity responseEntity=response.getEntity();
        System.out.println(responseEntity);
        cookieStr = response.getFirstHeader("Set-Cookie").getValue();
        System.out.println(cookieStr);
    }
  1. 创建wiki页面(注意URL的最后一项,NewPage表示的是创建的页面标识,不同的页面此值应不同,否则会覆盖)
    public void testNewPage() throws Exception{
        Page page =new Page();
        page.setTitle("testPage");
        page.setSyntax("xwiki/2.0");
        page.setContent("This is a testPage");
        page.setId("xwiki:Main.testPage");
        CloseableHttpClient httpclient = HttpClients.createDefault();
        JAXBContext context = JAXBContext.newInstance("org.xwiki.rest.model.jaxb");
        Marshaller marshaller=context.createMarshaller();
        Unmarshaller unmarshaller = context.createUnmarshaller();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        marshaller.marshal( page, out );

        InputStreamEntity inputStreamEntity=new InputStreamEntity(new ByteArrayInputStream(out.toByteArray()));

        HttpUriRequest httpPost = RequestBuilder.put()
                .setUri(new URI("http://172.16.200.220:8082/xwiki/rest/wikis/xwiki/spaces/Main/pages/NewPage"))
                .setEntity(inputStreamEntity)
                .setHeader("Content-Type", "application/xml")
                .setHeader("Cookie", cookieStr).build();
        CloseableHttpResponse response = httpclient.execute(httpPost);
        System.out.println(response.getStatusLine().getStatusCode());
        HttpEntity responseEntity=response.getEntity();
        System.out.println(responseEntity);
        if(response.getStatusLine().getStatusCode()<400){
            Page responsePage = (Page) unmarshaller.unmarshal(responseEntity.getContent());
            System.out.println(new Gson().toJson(responsePage));
        }
    }
  1. 修改wiki,使用如上第三步方式,只要url不变,再提交都是修改,只有第一次提交是创建

你可能感兴趣的:(java,wiki,xwiki,restclient)