PageRenderTime 49ms CodeModel.GetById 20ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 1ms

/razpub/src/com/razie/pub/comms/MoreComms.java

http://razpub.googlecode.com/
Java | 157 lines | 82 code | 18 blank | 57 comment | 4 complexity | 890aca9f39fc1e0f3d533e5cb7444b6d MD5 | raw file
  1/**
  2 * Razvan's public code. 
  3 * Copyright 2008 based on Apache license (share alike) see LICENSE.txt for details.
  4 */
  5package com.razie.pub.comms;
  6
  7import java.io.BufferedReader;
  8import java.io.File;
  9import java.io.IOException;
 10import java.io.InputStream;
 11import java.io.InputStreamReader;
 12import java.io.OutputStream;
 13import java.io.OutputStreamWriter;
 14import java.net.MalformedURLException;
 15import java.net.Socket;
 16import java.net.URL;
 17import java.net.URLConnection;
 18import java.util.List;
 19
 20import org.xml.sax.InputSource;
 21import org.xml.sax.SAXException;
 22
 23import razie.assets.AssetLocation;
 24import razie.base.AttrAccess;
 25
 26import com.razie.pub.base.data.ByteArray;
 27import com.razie.pub.base.log.Log;
 28import com.sun.org.apache.xerces.internal.parsers.DOMParser;
 29
 30/**
 31 * communications utils
 32 * 
 33 * TODO detailed docs
 34 * 
 35 * @author razvanc99
 36 * 
 37 */
 38public class MoreComms extends Comms {
 39
 40    /**
 41     * Stream the response of a URL.
 42     * 
 43     * @param url can be local or remote
 44     * @return a string containing the text read from the URL. can be the result of a servlet, a web
 45     *         page or the contents of a local file. It's null if i couldn't read the file.
 46     */
 47    public static InputStream poststreamUrl(String url, AttrAccess httpArgs, String content) {
 48        try {
 49            InputStream in = null;
 50           
 51            // parse url to invoke sendPOST
 52            AssetLocation temploc = new AssetLocation (url); 
 53            String cmd = "POST " + url.replace(temploc.toHttp(), "")+ " HTTP/1.1";
 54            
 55                Socket remote =  HttpHelper.sendPOST(temploc.getHost(), Integer.decode(temploc.getPort()), 
 56                        cmd, httpArgs, content);
 57                in = remote.getInputStream();
 58               
 59                // TODO interpret the http response...error codes etc
 60//                if (!resCode.endsWith("200 OK")) {
 61//                    String msg = "Could not fetch data from url " + url + ", resCode=" + resCode;
 62//                    logger.trace(3, msg);
 63//                    CommRtException rte = new CommRtException(msg);
 64//                    if (uc.getContentType().endsWith("xml")) {
 65//                        DOMParser parser = new DOMParser();
 66//                        try {
 67//                            parser.parse(new InputSource(in));
 68//                        } catch (SAXException e) {
 69//                            RuntimeException iex = new CommRtException("Error while processing document at "
 70//                                    + url);
 71//                            iex.initCause(e);
 72//                            throw iex;
 73//                        }
 74//                    }
 75//                    throw rte;
 76//                }
 77            return in;
 78        } catch (MalformedURLException e) {
 79            throw new IllegalArgumentException(e);
 80        } catch (IOException e1) {
 81            // server/node down
 82            throw new RuntimeException("Connection exception for url="+url, e1);
 83        }
 84    }
 85
 86    /**
 87     * Stream the response of a URL.
 88     * 
 89     * @param url can be local or remote
 90     * @return a string containing the text read from the URL. can be the result of a servlet, a web
 91     *         page or the contents of a local file. It's null if i couldn't read the file.
 92     */
 93    public static InputStream xpoststreamUrl2(String url, AttrAccess httpArgs, String content) {
 94        try {
 95            InputStream in = null;
 96                URLConnection uc = (new URL(url)).openConnection();
 97                // see http://www.exampledepot.com/egs/java.net/Post.html
 98                uc.setDoOutput(true);
 99                
100                OutputStreamWriter wr = new OutputStreamWriter(uc.getOutputStream());
101                wr.write(content);
102                wr.flush();
103                
104                
105                logger.trace(3, "hdr: ", uc.getHeaderFields());
106                String resCode = uc.getHeaderField(0);
107                in = uc.getInputStream();
108
109
110
111                if (!resCode.endsWith("200 OK")) {
112                    String msg = "Could not fetch data from url " + url + ", resCode=" + resCode;
113                    logger.trace(3, msg);
114                    RuntimeException rte = new RuntimeException(msg);
115                    if (uc.getContentType().endsWith("xml")) {
116                        DOMParser parser = new DOMParser();
117                        try {
118                            parser.parse(new InputSource(in));
119                        } catch (SAXException e) {
120                            RuntimeException iex = new RuntimeException("Error while processing document at "
121                                    + url);
122                            iex.initCause(e);
123                            throw iex;
124                        }
125                    }
126                    throw rte;
127                }
128            return in;
129        } catch (MalformedURLException e) {
130            RuntimeException iex = new IllegalArgumentException();
131            iex.initCause(e);
132            throw iex;
133        } catch (IOException e1) {
134            // server/node down
135            throw new RuntimeException("Connection exception for url="+url, e1);
136        }
137    }
138
139    /**
140     * Serialize to string the response of a URL, via POST rather than GET.
141     * 
142     * @param url MUST be remotec
143     * @param httpArgs the args to be sent with the HTTP request
144     * @param content the content of the POST
145     * @return a string containing the text read from the URL. can be the result of a servlet, a web
146     *         page or the contents of a local file. It's null if i couldn't read the file.
147     */
148    public static String readUrlPOST(String url, AttrAccess httpArgs, String content) {
149        InputStream s = poststreamUrl(url, httpArgs, content);
150        if (s == null) {
151            return null;
152        }
153        return readStream(s);
154    }
155
156    static Log logger = Log.create(Comms.class.getName());
157}