Java tutorial
/** * personium.io * Copyright 2014 FUJITSU LIMITED * * Licensed 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 com.fujitsu.dc.test.jersey; import static org.junit.Assert.fail; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.zip.GZIPInputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import org.w3c.dom.Document; import org.xml.sax.SAXException; /** * DAV??. */ public class DcResponse { /** ?. */ private HttpResponse response; /** * . */ public DcResponse() { } /** * . * @param resObj ? */ public DcResponse(final HttpResponse resObj) { this.response = resObj; } /** * ??. * @return */ public final int getStatusCode() { return response.getStatusLine().getStatusCode(); } /** * ???????. * @return ? */ public final Header[] getResponseAllHeaders() { return response.getAllHeaders(); } /** * ?????. * @param value ????? * @return ? */ public final Header[] getResponseHeaders(final String value) { return response.getHeaders(value); } /** * ?????. * @param name ????? * @return ? */ public final String getFirstHeader(String name) { try { return response.getFirstHeader(name).getValue(); } catch (Exception e) { return null; } } /** * ???. * @return */ public final InputStream bodyAsStream() { HttpEntity ent = response.getEntity(); if (ent == null) { return null; } try { return ent.getContent(); } catch (IllegalStateException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } } /** * ???. * @return * @throws DaoException DAO */ public final String bodyAsString() throws DaoException { return this.bodyAsString("utf-8"); } /** * ???. * @param enc * @return * @throws DaoException DAO */ public final String bodyAsString(final String enc) throws DaoException { InputStream is = null; InputStreamReader isr = null; BufferedReader reader = null; try { is = this.getResponseBodyInputStream(response); isr = new InputStreamReader(is, enc); reader = new BufferedReader(isr); StringBuffer sb = new StringBuffer(); int chr; while ((chr = reader.read()) != -1) { sb.append((char) chr); } return sb.toString(); } catch (IOException e) { throw DaoException.create("io exception", 0); } finally { try { if (is != null) { is.close(); } if (isr != null) { isr.close(); } if (reader != null) { reader.close(); } } catch (Exception e) { throw DaoException.create("io exception", 0); } finally { try { if (isr != null) { isr.close(); } if (reader != null) { reader.close(); } } catch (Exception e2) { throw DaoException.create("io exception", 0); } finally { try { if (reader != null) { reader.close(); } } catch (Exception e3) { throw DaoException.create("io exception", 0); } } } } } /** * ?JSON??. * @return JSON */ public final JSONObject bodyAsJson() { String res = null; try { res = bodyAsString(); } catch (DaoException e) { fail(e.getMessage()); } JSONObject jsonobject = null; try { jsonobject = (JSONObject) new JSONParser().parse(res); } catch (ParseException e) { fail(e.getMessage()); } return jsonobject; } /** * ?XML??. * @return XML DOM */ public final Document bodyAsXml() { String str = ""; try { str = bodyAsString(); } catch (DaoException e) { fail(e.getMessage()); } DocumentBuilder builder = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); builder = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { fail(e.getMessage()); } Document document = null; InputStream is = new ByteArrayInputStream(str.getBytes()); try { document = builder.parse(is); } catch (SAXException e) { fail(e.getMessage()); } catch (IOException e) { fail(e.getMessage()); } return document; } /** * ?????. * @param res Response * @return * @throws IOException IO */ protected final InputStream getResponseBodyInputStream(final HttpResponse res) throws IOException { // GZip ?????? Header[] contentEncodingHeaders = res.getHeaders("Content-Encoding"); if (contentEncodingHeaders.length > 0 && "gzip".equalsIgnoreCase(contentEncodingHeaders[0].getValue())) { return new GZIPInputStream(res.getEntity().getContent()); } else { return res.getEntity().getContent(); } } }