Example usage for javax.net.ssl HttpsURLConnection getErrorStream

List of usage examples for javax.net.ssl HttpsURLConnection getErrorStream

Introduction

In this page you can find the example usage for javax.net.ssl HttpsURLConnection getErrorStream.

Prototype

public InputStream getErrorStream() 

Source Link

Document

Returns the error stream if the connection failed but the server sent useful data nonetheless.

Usage

From source file:org.liberty.android.fantastischmemo.downloader.google.WorksheetFactory.java

public static List<Worksheet> getWorksheets(Spreadsheet spreadsheet, String authToken)
        throws XmlPullParserException, IOException {
    String worksheetAddress = "https://spreadsheets.google.com/feeds/worksheets/" + spreadsheet.getId()
            + "/private/full?access_token=" + authToken;
    URL url = new URL(worksheetAddress);
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

    if (conn.getResponseCode() / 100 >= 3) {
        String s = new String(IOUtils.toByteArray(conn.getErrorStream()));
        throw new RuntimeException(s);
    }/*  ww w .  j a va  2  s.c  o  m*/
    List<Worksheet> worksheetList = EntryFactory.getEntries(Worksheet.class, conn.getInputStream());
    return worksheetList;
}

From source file:org.liberty.android.fantastischmemo.downloader.google.SpreadsheetFactory.java

public static List<Spreadsheet> getSpreadsheets(String authToken) throws XmlPullParserException, IOException {
    URL url = new URL(
            "https://spreadsheets.google.com/feeds/spreadsheets/private/full?access_token=" + authToken);
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    //conn.addRequestProperty("Authorization", "GoogleLogin auth=" + authToken);

    if (conn.getResponseCode() / 100 >= 3) {
        String s = new String(IOUtils.toByteArray(conn.getErrorStream()));
        throw new IOException(s);
    }// ww  w . ja  v  a  2 s.  com

    List<Spreadsheet> spreadsheetList = EntryFactory.getEntries(Spreadsheet.class, conn.getInputStream());
    return spreadsheetList;
}

From source file:org.liberty.android.fantastischmemo.downloader.google.DocumentFactory.java

public static List<Document> findDocuments(String title, String authToken) throws Exception {
    URL url = new URL("https://www.googleapis.com/drive/v2/files?q="
            + URLEncoder.encode("title = '" + title + "'", "UTF-8"));

    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setRequestProperty("Authorization", "Bearer " + authToken);

    if (conn.getResponseCode() >= 300) {
        String s = new String(IOUtils.toByteArray(conn.getErrorStream()));
        throw new RuntimeException(s);
    }//ww w  . j  a  va2 s  .co m

    List<Document> documentList = EntryFactory.getEntriesFromDriveApi(Document.class, conn.getInputStream());
    return documentList;
}

From source file:org.liberty.android.fantastischmemo.downloader.google.CellsFactory.java

public static Cells getCells(Spreadsheet spreadsheet, Worksheet worksheet, String authToken)
        throws XmlPullParserException, IOException {
    URL url = new URL("https://spreadsheets.google.com/feeds/cells/" + spreadsheet.getId() + "/"
            + worksheet.getId() + "/private/full?access_token=" + authToken);
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

    if (conn.getResponseCode() / 100 >= 3) {
        String s = new String(IOUtils.toByteArray(conn.getErrorStream()));
        throw new RuntimeException(s);
    }//from  ww  w . j a v a 2 s.  c o  m

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser xpp = factory.newPullParser();
    xpp.setInput(new BufferedReader(new InputStreamReader(conn.getInputStream())));

    int eventType = xpp.getEventType();

    List<Row> rowList = new ArrayList<Row>(100);
    Row row = null;
    String lastTag = "";
    int currentRow = 1;
    int currentCol = 1;
    Cells cells = new Cells();
    while (eventType != XmlPullParser.END_DOCUMENT) {

        if (eventType == XmlPullParser.START_DOCUMENT) {
        } else if (eventType == XmlPullParser.START_TAG) {
            lastTag = xpp.getName();
            if (xpp.getName().equals("cell")) {
                currentRow = Integer.valueOf(xpp.getAttributeValue(null, "row"));
                currentCol = Integer.valueOf(xpp.getAttributeValue(null, "col"));
            }
        } else if (eventType == XmlPullParser.END_TAG) {
            if (xpp.getName().equals("entry")) {
                rowList.add(row);
                row = null;
            }
        } else if (eventType == XmlPullParser.TEXT) {
            if (lastTag.equals("cell")) {
                cells.addCell(currentRow, currentCol, xpp.getText());
            }
            if (lastTag.equals("title") && Strings.isNullOrEmpty(cells.getWorksheetName())) {
                cells.setWorksheetName(xpp.getText());
            }
        }
        eventType = xpp.next();
    }
    return cells;
}

From source file:org.liberty.android.fantastischmemo.downloader.google.FolderFactory.java

public static List<Folder> getFolders(String authToken) throws XmlPullParserException, IOException {
    URL url = new URL("https://www.googleapis.com/drive/v2/files?q="
            + URLEncoder.encode("mimeType = 'application/vnd.google-apps.folder'", "UTF-8"));
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setRequestProperty("Authorization", "Bearer " + authToken);
    if (conn.getResponseCode() / 100 >= 3) {
        String s = new String(IOUtils.toByteArray(conn.getErrorStream()));
        throw new IOException(s);
    }//w  w w  .j av  a2s . c om

    List<Folder> folderList = EntryFactory.getEntriesFromDriveApi(Folder.class, conn.getInputStream());

    return folderList;
}

From source file:org.liberty.android.fantastischmemo.downloader.google.DocumentFactory.java

public static void deleteDocument(Document document, String authToken) throws IOException {
    URL url = new URL("https://www.googleapis.com/drive/v2/files/" + document.getId());
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setRequestProperty("Authorization", "Bearer " + authToken);

    conn.setRequestMethod("DELETE");

    if (conn.getResponseCode() / 100 >= 3) {
        String s = new String(IOUtils.toByteArray(conn.getErrorStream()));
        throw new RuntimeException(s);
    }// w ww .j  a va  2s  .  c o m
}

From source file:org.liberty.android.fantastischmemo.downloader.google.WorksheetFactory.java

public static void deleteWorksheet(Spreadsheet spreadsheet, Worksheet worksheet, String authToken)
        throws Exception {
    String requestUrl = "https://spreadsheets.google.com/feeds/worksheets/" + spreadsheet.getId()
            + "/private/full/" + worksheet.getId() + "/0" + "?access_token=" + authToken;
    URL url = new URL(requestUrl);
    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setRequestMethod("DELETE");
    conn.addRequestProperty("If-Match", "*");
    if (conn.getResponseCode() / 100 >= 3) {
        String s = new String(IOUtils.toByteArray(conn.getErrorStream()));
        throw new Exception(s);
    }/*  w w w  .jav a  2 s . c  o  m*/
}

From source file:org.liberty.android.fantastischmemo.downloader.quizlet.lib.java

/**
 * Make API call to Quizlet server with oauth
 *
 * @param url/*from  w w w.  j  a  v a 2  s.  c  o m*/
 *            API call endpoint
 * @param authToken
 *            oauth auth token
 * @return Response of API call
 * @throws IOException
 *             If http response code is not 2xx
 */
public static InputStream makeApiCall(URL url, String authToken) throws IOException {
    HttpsURLConnection conn = null;
    try {
        conn = (HttpsURLConnection) url.openConnection();
        if (authToken != null) {
            conn.addRequestProperty("Authorization", "Bearer " + authToken);
        }

        InputStream response = conn.getInputStream();
        if (conn.getResponseCode() / 100 >= 3) {
            response = conn.getErrorStream();
        }
        return response;
    } finally {
        //conn.disconnect();
    }
}

From source file:org.liberty.android.fantastischmemo.downloader.google.WorksheetFactory.java

public static Worksheet createWorksheet(Spreadsheet spreadsheet, String title, int row, int col,
        String authToken) throws Exception {
    URL url = new URL("https://spreadsheets.google.com/feeds/worksheets/" + spreadsheet.getId()
            + "/private/full?access_token=" + authToken);

    String payload = "<entry xmlns=\"http://www.w3.org/2005/Atom\""
            + " xmlns:gs=\"http://schemas.google.com/spreadsheets/2006\">" + "<title>"
            + URLEncoder.encode(title, "UTF-8") + "</title>" + "<gs:rowCount>" + row + "</gs:rowCount>"
            + "<gs:colCount>" + col + "</gs:colCount>" + "</entry>";

    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
    conn.setRequestMethod("POST");
    conn.setDoInput(true);//  w  w w  .j a va 2  s.c  o m
    conn.setDoOutput(true);
    conn.addRequestProperty("Content-Type", "application/atom+xml");
    conn.addRequestProperty("Content-Length", "" + payload.getBytes("UTF-8").length);
    OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
    out.write(payload);
    out.close();

    if (conn.getResponseCode() / 100 >= 3) {
        String s = new String(IOUtils.toByteArray(conn.getErrorStream()));
        throw new Exception(s);
    }

    List<Worksheet> worksheets = getWorksheets(spreadsheet, authToken);
    for (Worksheet worksheet : worksheets) {
        if (title.equals(worksheet.getTitle())) {
            return worksheet;
        }
    }
    throw new IllegalStateException("Worksheet lookup failed. Worksheet is not created properly.");
}

From source file:com.coinprism.model.APIClient.java

private static String getHttpResponse(HttpsURLConnection connection) throws IOException, APIException {
    int responseCode = connection.getResponseCode();

    if (responseCode < 400) {
        InputStream inputStream = new BufferedInputStream(connection.getInputStream());

        return readStream(inputStream);
    } else {/*  w w w.  jav a  2  s  . c  o m*/
        InputStream inputStream = new BufferedInputStream(connection.getErrorStream());
        String response = readStream(inputStream);
        try {
            JSONObject error = new JSONObject(response);
            String errorCode = error.getString("ErrorCode");
            String subCode = error.optString("SubCode");

            throw new APIException(errorCode, subCode);
        } catch (JSONException exception) {
            throw new IOException(exception.getMessage());
        }
    }
}