Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.pubnub.api;
  
 import java.net.URL;
 
 import  org.json.JSONArray;
 import  org.json.JSONException;
 import  org.json.JSONObject;
 
 import static com.pubnub.api.PubnubError.*;
 
 class HttpClientCore extends HttpClient {
     private int requestTimeout = 310000;
     private int connectionTimeout = 5000;
     protected static Logger log = new Logger(Worker.class);
 
     private void init() {
         HttpURLConnection.setFollowRedirects(true);
     }
 
     public HttpClientCore(int connectionTimeoutint requestTimeoutHashtable headers) {
         init();
         this.setRequestTimeout(requestTimeout);
         this.setConnectionTimeout(connectionTimeout);
         this. = headers;
     }
 
     public int getRequestTimeout() {
         return ;
     }
 
     public void setRequestTimeout(int requestTimeout) {
         this. = requestTimeout;
     }
 
     public int getConnectionTimeout() {
         return ;
     }
 
     public void setConnectionTimeout(int connectionTimeout) {
         this. = connectionTimeout;
     }
 
     public boolean isRedirect(int rc) {
         return (rc == .
                 || rc == . || rc == .);
     }
 
     public boolean checkResponse(int rc) {
         return (rc == . || isRedirect(rc));
     }
 
     public boolean checkResponseSuccess(int rc) {
         return (rc == .);
     }
 
     private static String readInput(InputStream inthrows IOException {
         ByteArrayOutputStream out = new ByteArrayOutputStream();
         byte bytes[] = new byte[1024];
 
         int n = in.read(bytes);
 
         while (n != -1) {
             out.write(bytes, 0, n);
             n = in.read(bytes);
         }
 
         return new String(out.toString());
     }
 
     public HttpResponse fetch(String urlthrows PubnubExceptionSocketTimeoutException {
         return fetch(urlnull);
     }
 
     public synchronized HttpResponse fetch(String urlHashtable headers)
             throws PubnubExceptionSocketTimeoutException {
         URL urlobj = null;
         try {
             urlobj = new URL(url);
         } catch (MalformedURLException e3) {
             throw new PubnubException(getErrorObject(,url));
         }
         try {
              = (HttpURLConnectionurlobj.openConnection();
         } catch (IOException e2) {
             throw new PubnubException(getErrorObject(url));
         }
         try {
            .setRequestMethod("GET");
        } catch (ProtocolException e1) {
            throw new PubnubException();
        }
        if ( != null) {
            Enumeration en = .keys();
            while (en.hasMoreElements()) {
                String key = (Stringen.nextElement();
                String val = (String.get(key);
                .addRequestProperty(keyval);
            }
        }
        if (headers != null) {
            Enumeration en = headers.keys();
            while (en.hasMoreElements()) {
                String key = (Stringen.nextElement();
                String val = (Stringheaders.get(key);
                .addRequestProperty(keyval);
            }
        }
        try {
            .connect();
        }
        catch (SocketTimeoutException  e) {
            throw e;
        }
        catch (IOException e) {
            throw new PubnubException(getErrorObject(url));
        }
        int rc = .;
        try {
            rc = .getResponseCode();
        } catch (IOException e) {
            throw new PubnubException(getErrorObject(url));
        }
        InputStream is = null;
        String encoding = .getContentEncoding();
        if(encoding == null || !encoding.equals("gzip")) {
            try {
                is = .getInputStream();
            } catch (IOException e) {
                if (rc == .)
                    throw new PubnubException(getErrorObject(, 1, url));
                is = .getErrorStream();
            }
        } else {
            try {
                is = new GZIPInputStream(.getInputStream());
            } catch (IOException e) {
                if (rc == .)
                    throw new PubnubException(getErrorObject(, 2, url));
                is = .getErrorStream();
            }
        }
        String page = null;
        try {
            page = readInput(is);
        } catch (IOException e) {
            throw new PubnubException(getErrorObject(url));
        }
        switch (rc) {
        case .:
            throw new PubnubException(getErrorObject(page));
            throw new PubnubException(getErrorObject(page));
        case .:
            try {
                JSONArray jsarr = new JSONArray(page);
                String error = jsarr.get(1).toString();
                throw new PubnubException(getErrorObject(, 1, error));
            } catch (JSONException e) {
                JSONObject jso;
                try {
                    jso = new JSONObject(page);
                    throw new PubnubException(getErrorObject(, 2, jso.toString()));
                } catch (JSONException e1) {
                    throw new PubnubException(getErrorObject(, 2));
                }
            }
        case .:
            throw new PubnubException(getErrorObject(url));
            throw new PubnubException(getErrorObject(url));
            throw new PubnubException(getErrorObject(url));
            throw new PubnubException(getErrorObject(url));
        default:
            break;
        }
        .verbose("URL = " + url + " : RESPONSE = " + page);
        return new HttpResponse(rcpage);
    }
    public boolean isOk(int rc) {
        return (rc == .);
    }
    public void shutdown() {
        if ( != null.disconnect();
    }
New to GrepCode? Check out our FAQ X