⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 httppostpetstoreproxy.java

📁 sun公司的petstore,一个J2ee的示例应用程序.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright 2002 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright *   notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright *   notice, this list of conditions and the following disclaimer in *   the documentation and/or other materials provided with the *   distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that Software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of * any nuclear facility. */package com.sun.j2ee.blueprints.admin.client;import java.util.Date;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.text.ParsePosition;import java.util.ArrayList;import java.net.URL;import java.net.MalformedURLException;import java.net.HttpURLConnection;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.InputStreamReader;import java.io.DataOutputStream;import java.io.BufferedReader;import java.io.StringReader;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.NamedNodeMap;import org.xml.sax.InputSource;public class HttpPostPetStoreProxy implements PetStoreProxy {    private static final String requestType =        "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"yes\"?>\n";    private URL url = null;    private DocumentBuilder documentBuilder = null;    private DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");    public HttpPostPetStoreProxy() {    }    public void setup(String host, String port, String sessionID) {        // TBD assert host, port        try {            url = new URL("http://" + host + ":" + port                + "/admin/ApplRequestProcessor;jsessionid=" + sessionID);        } catch(MalformedURLException e) {            e.printStackTrace();            // TBD deal with exception - rethrow to main()        }        try {            documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();        } catch (Exception e) {            e.printStackTrace();            // TBD deal with exception - rethrow to main()        }    }    private Document doHttpPost(String request) {        InputStream ist = null;        OutputStream ost = null;        HttpURLConnection uc = null;        try {            /* Initialize the HttpURLConnection */            uc = (HttpURLConnection)(url.openConnection());            uc.setRequestMethod("POST");            uc.setDoOutput(true);            uc.setUseCaches(false);            uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");            uc.setRequestProperty("JSESSIONID", "HAHAHAHA");            uc.connect();            /* Send the post request */            ost = uc.getOutputStream();            DataOutputStream out = new DataOutputStream(ost);            out.writeBytes(request);            out.flush();            out.close();            ost = null;            /* Read and print the response */            ist = uc.getInputStream();            BufferedReader in = new BufferedReader(new InputStreamReader(ist));            StringBuffer sb = new StringBuffer();            char[] c = new char[1];            while (in.read(c, 0, 1) == 1) {                sb.append(c[0]);            }            InputSource src = new InputSource(new StringReader(sb.toString()));            return documentBuilder.parse(src);        } catch (Exception e) {            if (uc != null) {                uc.disconnect();            }            return null;        } finally { // Carefully close the input and output streams            if (ost != null) {                try {                    ost.close();                }                catch (IOException ignore) {                }            }            if (ist != null) {                try {                    ist.close();                }                catch (IOException ignore) {                }            }        }    }    private String genRequest(StringBuffer body) {        StringBuffer sb = new StringBuffer(requestType);        sb.append("<Request>\n");        sb.append(body.toString());        sb.append("</Request>\n");        return sb.toString();    }    /**     * Return the value of the first child of this node.  For example, given     * a node like "<foo>bar</foo>", return "bar".     */    private String getBody(Node node) {        try {            return node.getFirstChild().getNodeValue();        } catch (Exception e) {            e.printStackTrace();            return null;        }    }    /**     * Return the body of the first element with the specified tag.  We     * assume that the element has one text node child.  If anything goes     * wrong, return null.     */    private String getBody(Document doc, String tag) {        return getBody(doc.getElementsByTagName(tag).item(0));    }    /**     * Return the date value of the first child of this node.  See the Document     * overload of this method for more information.     */    private Date getDate(Node node) {        try {            String s = getBody(node);            return dateFormat.parse(s, new ParsePosition(0));        }        catch (Exception e) {            e.printStackTrace();            return null;        }    }    /**     * Return the Date value of the first element with the specified tag.     * The body of the element is assumed to be a date in the ISO 8601 format     * used by the ICalendar server.  For example: "20010331T000000Z", is interpreted     * as "Fri Mar 30 16:00:00 PST 2001".  Note that the calendar start/end     * dates are assumed to be in the GMT time zone.     */    private Date getDate(Document doc, String tag) {        return getDate(doc.getElementsByTagName(tag).item(0));    }    /**     * Return the integer value of the first child of this node.     */    private int getInt(Node node) {        try {            String s = getBody(node);            return Integer.parseInt(s);        }        catch (Exception e) {            e.printStackTrace();            return -1;        }    }    /**     * Return the float value of the first child of this node.     */    private float getFloat(Node node) {        String s = null;        try {            s = getBody(node);            return Float.parseFloat(s);        }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -