Example usage for java.io OutputStreamWriter close

List of usage examples for java.io OutputStreamWriter close

Introduction

In this page you can find the example usage for java.io OutputStreamWriter close.

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:com.techventus.server.voice.Voice.java

/**
 * Cancel a call that was just placed.//from w  ww.ja  v a2 s  .  c  om
 * 
 * @param originNumber
 *            the origin number
 * @param destinationNumber
 *            the destination number
 * @param phoneType
 *            the phone type
 * @return the string
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public String cancelCall(String originNumber, String destinationNumber, String phoneType) throws IOException {
    String out = "";
    String calldata = "";
    calldata += URLEncoder.encode("outgoingNumber", enc) + "=" + URLEncoder.encode("undefined", enc);
    calldata += "&" + URLEncoder.encode("forwardingNumber", enc) + "=" + URLEncoder.encode("undefined", enc);

    calldata += "&" + URLEncoder.encode("cancelType", enc) + "=" + URLEncoder.encode("C2C", enc);
    calldata += "&" + URLEncoder.encode("_rnr_se", enc) + "=" + URLEncoder.encode(rnrSEE, enc);
    // POST /voice/call/connect/ outgoingNumber=[number to
    // call]&forwardingNumber=[forwarding
    // number]&subscriberNumber=undefined&remember=0&_rnr_se=[pull from
    // page]
    URL callURL = new URL("https://www.google.com/voice/b/0/call/cancel/");

    URLConnection callconn = callURL.openConnection();
    callconn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken);
    callconn.setRequestProperty("User-agent", USER_AGENT);

    callconn.setDoOutput(true);
    OutputStreamWriter callwr = new OutputStreamWriter(callconn.getOutputStream());
    callwr.write(calldata);
    callwr.flush();

    BufferedReader callrd = new BufferedReader(new InputStreamReader(callconn.getInputStream()));

    String line;
    while ((line = callrd.readLine()) != null) {
        out += line + "\n\r";

    }

    callwr.close();
    callrd.close();

    if (out.equals("")) {
        throw new IOException("No Response Data Received.");
    }

    return out;

}

From source file:com.techventus.server.voice.Voice.java

/**
 * Enables/disables the call Announcement setting (general for all phones).
 *
 * @param announceCaller <br/>/*from  w w  w.  j av  a2s .co m*/
 * true Announces caller's name and gives answering options <br/>
 * false Directly connects calls when phones are answered
 * @return the raw response of the disable action.
 * @throws IOException Signals that an I/O exception has occurred.
 */
public String setCallPresentation(boolean announceCaller) throws IOException {
    String out = "";

    URL requestURL = new URL(generalSettingsURLString);
    /** 0 for enable, 1 for disable **/
    String announceCallerStr = "";

    if (announceCaller) {
        announceCallerStr = "0";
        if (PRINT_TO_CONSOLE)
            System.out.println("Turning caller announcement on.");
    } else {
        announceCallerStr = "1";
        if (PRINT_TO_CONSOLE)
            System.out.println("Turning caller announcement off.");
    }

    String paraString = "";
    paraString += URLEncoder.encode("directConnect", enc) + "=" + URLEncoder.encode(announceCallerStr, enc);
    paraString += "&" + URLEncoder.encode("_rnr_se", enc) + "=" + URLEncoder.encode(rnrSEE, enc);

    URLConnection conn = requestURL.openConnection();
    conn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken);
    conn.setRequestProperty("User-agent", USER_AGENT);

    conn.setDoOutput(true);
    conn.setDoInput(true);

    OutputStreamWriter callwr = new OutputStreamWriter(conn.getOutputStream());
    callwr.write(paraString);
    callwr.flush();

    BufferedReader callrd = new BufferedReader(new InputStreamReader(conn.getInputStream()));

    String line;
    while ((line = callrd.readLine()) != null) {
        out += line + "\n\r";
    }

    callwr.close();
    callrd.close();

    if (out.equals("")) {
        throw new IOException("No Response Data Received.");
    }

    return out;
}

From source file:com.votingcentral.services.maxmind.MaxMindGeoLocationService.java

public MaxMindLocationTO getLocation(String ipAddress) {
    MaxMindLocationTO mto = new MaxMindLocationTO();
    String specificData = data + FastURLEncoder.encode(ipAddress, "UTF-8");
    URL url;/*from   www .  j  a  va 2s . c  o  m*/
    OutputStreamWriter wr = null;
    BufferedReader rd = null;
    try {
        url = new URL(surl);
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(specificData);
        wr.flush();
        // Get the response
        rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line;
        while ((line = rd.readLine()) != null) {
            String[] values = StringUtils.split(line, ",");
            int maxIndex = values.length - 1;
            if (maxIndex >= 0) {
                mto.setISO3166TwoLetterCountryCode(values[0]);
            }
            if (maxIndex >= 1) {
                mto.setRegionCode(values[1]);
            }
            if (maxIndex >= 2) {
                mto.setCity(values[2]);
            }
            if (maxIndex >= 3) {
                mto.setPostalCode(values[3]);
            }
            if (maxIndex >= 4) {
                mto.setLatitude(values[4]);
            }
            if (maxIndex >= 5) {
                mto.setLongitude(values[5]);
            }
            if (maxIndex >= 6) {
                mto.setMetropolitanCode(values[6]);
            }
            if (maxIndex >= 7) {
                mto.setAreaCode(values[7]);
            }
            if (maxIndex >= 8) {
                mto.setIsp(values[8]);
            }
            if (maxIndex >= 9) {
                mto.setOranization(values[9]);
            }
            if (maxIndex >= 10) {
                mto.setError(MaxMindErrorsEnum.get(values[10]));
            }
        }
    } catch (MalformedURLException e) {
        log.fatal("Issue calling Maxmind", e);
        mto.setError(MaxMindErrorsEnum.VC_INTERNAL_ERROR);
    } catch (IOException e) {
        log.fatal("Issue reading Maxmind", e);
        mto.setError(MaxMindErrorsEnum.VC_INTERNAL_ERROR);
    } finally {
        if (wr != null) {
            try {
                wr.close();
            } catch (IOException e1) {
                log.fatal("Issue closing Maxmind", e1);
                mto.setError(MaxMindErrorsEnum.VC_INTERNAL_ERROR);
            }
        }
        if (rd != null) {
            try {
                rd.close();
            } catch (IOException e1) {
                log.fatal("Issue closing Maxmind", e1);
                mto.setError(MaxMindErrorsEnum.VC_INTERNAL_ERROR);
            }
        }
    }
    return mto;
}

From source file:com.techventus.server.voice.Voice.java

/**
 * Mark a Conversation with a known Message ID as read.
 *
 * @param msgID the msg id//from  w w w.  j a v  a 2  s.  c om
 * @return the string
 * @throws IOException Signals that an I/O exception has occurred.
 */
public String markAsRead(String msgID) throws IOException {
    String out = "";
    StringBuffer calldata = new StringBuffer();

    // POST /voice/inbox/mark/ 
    // messages=[messageID]
    // &read=1
    // &_rnr_se=[pull from page]

    calldata.append("messages=");
    calldata.append(URLEncoder.encode(msgID, enc));
    calldata.append("&read=1");
    calldata.append("&_rnr_se=");
    calldata.append(URLEncoder.encode(rnrSEE, enc));

    URL callURL = new URL(markAsReadString);

    URLConnection callconn = callURL.openConnection();
    callconn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken);
    callconn.setRequestProperty("User-agent", USER_AGENT);

    callconn.setDoOutput(true);
    OutputStreamWriter callwr = new OutputStreamWriter(callconn.getOutputStream());

    callwr.write(calldata.toString());
    callwr.flush();

    BufferedReader callrd = new BufferedReader(new InputStreamReader(callconn.getInputStream()));

    String line;
    while ((line = callrd.readLine()) != null) {
        out += line + "\n\r";

    }

    callwr.close();
    callrd.close();

    if (out.equals("")) {
        throw new IOException("No Response Data Received.");
    }

    return out;
}

From source file:com.techventus.server.voice.Voice.java

/**
 * Mark a Conversation with a known Message ID as unread.
 *
 * @param msgID the msg id/* w w w. j a v  a  2  s  .co m*/
 * @return the string
 * @throws IOException Signals that an I/O exception has occurred.
 */
public String markUnRead(String msgID) throws IOException {
    String out = "";
    StringBuffer calldata = new StringBuffer();

    // POST /voice/inbox/mark/ 
    // messages=[messageID]
    // &read=0
    // &_rnr_se=[pull from page]

    calldata.append("messages=");
    calldata.append(URLEncoder.encode(msgID, enc));
    calldata.append("&read=0");
    calldata.append("&_rnr_se=");
    calldata.append(URLEncoder.encode(rnrSEE, enc));

    URL callURL = new URL("https://www.google.com/voice/b/0/inbox/mark");

    URLConnection callconn = callURL.openConnection();
    callconn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken);
    callconn.setRequestProperty("User-agent", USER_AGENT);

    callconn.setDoOutput(true);
    OutputStreamWriter callwr = new OutputStreamWriter(callconn.getOutputStream());

    callwr.write(calldata.toString());
    callwr.flush();

    BufferedReader callrd = new BufferedReader(new InputStreamReader(callconn.getInputStream()));

    String line;
    while ((line = callrd.readLine()) != null) {
        out += line + "\n\r";

    }

    callwr.close();
    callrd.close();

    if (out.equals("")) {
        throw new IOException("No Response Data Received.");
    }

    return out;
}

From source file:com.techventus.server.voice.Voice.java

/**
 * Delete message./* w  ww .j  a v  a2s  .co m*/
 *
 * @param msgID the msg id
 * @return the string
 * @throws IOException Signals that an I/O exception has occurred.
 */
public String deleteMessage(String msgID) throws IOException {
    String out = "";
    StringBuffer calldata = new StringBuffer();

    // POST /voice/inbox/deleteMessages/
    // messages=[messageID]
    // &trash=1
    // &_rnr_se=[pull from page]

    calldata.append("messages=");
    calldata.append(URLEncoder.encode(msgID, enc));
    calldata.append("&trash=1");
    calldata.append("&_rnr_se=");
    calldata.append(URLEncoder.encode(rnrSEE, enc));

    URL callURL = new URL("https://www.google.com/voice/b/0/inbox/deleteMessages/");

    URLConnection callconn = callURL.openConnection();
    callconn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken);
    callconn.setRequestProperty("User-agent", USER_AGENT);

    callconn.setDoOutput(true);
    OutputStreamWriter callwr = new OutputStreamWriter(callconn.getOutputStream());

    callwr.write(calldata.toString());
    callwr.flush();

    BufferedReader callrd = new BufferedReader(new InputStreamReader(callconn.getInputStream()));

    String line;
    while ((line = callrd.readLine()) != null) {
        out += line + "\n\r";

    }

    callwr.close();
    callrd.close();

    if (out.equals("")) {
        throw new IOException("No Response Data Received.");
    }

    return out;
}

From source file:com.techventus.server.voice.Voice.java

/**
 * Place a call.// w  ww.ja  v  a  2 s .co  m
 * 
 * @param originNumber
 *            the origin number
 * @param destinationNumber
 *            the destination number
 * @param phoneType
 *            the phone type, this is a number such as 1,2,7 formatted as a String
 * @return the raw response string received from Google Voice.
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public String call(String originNumber, String destinationNumber, String phoneType) throws IOException {
    String out = "";
    StringBuffer calldata = new StringBuffer();

    // POST /voice/call/connect/ 
    // outgoingNumber=[number to call]
    // &forwardingNumber=[forwarding number]
    // &subscriberNumber=undefined
    // &phoneType=[phone type from google]
    // &remember=0
    // &_rnr_se=[pull from page]

    calldata.append("outgoingNumber=");
    calldata.append(URLEncoder.encode(destinationNumber, enc));
    calldata.append("&forwardingNumber=");
    calldata.append(URLEncoder.encode(originNumber, enc));
    calldata.append("&subscriberNumber=undefined");
    calldata.append("&phoneType=");
    calldata.append(URLEncoder.encode(phoneType, enc));
    calldata.append("&remember=0");
    calldata.append("&_rnr_se=");
    calldata.append(URLEncoder.encode(rnrSEE, enc));

    URL callURL = new URL("https://www.google.com/voice/b/0/call/connect/");

    URLConnection callconn = callURL.openConnection();
    callconn.setRequestProperty("Authorization", "GoogleLogin auth=" + authToken);
    callconn.setRequestProperty("User-agent", USER_AGENT);

    callconn.setDoOutput(true);
    OutputStreamWriter callwr = new OutputStreamWriter(callconn.getOutputStream());

    callwr.write(calldata.toString());
    callwr.flush();

    BufferedReader callrd = new BufferedReader(new InputStreamReader(callconn.getInputStream()));

    String line;
    while ((line = callrd.readLine()) != null) {
        out += line + "\n\r";

    }

    callwr.close();
    callrd.close();

    if (out.equals("")) {
        throw new IOException("No Response Data Received.");
    }

    return out;

}

From source file:com.wyp.module.controller.LicenseController.java

/**
 * wslicense//from  www. ja  va  2s  .  c  o  m
 * 
 * @param properties
 * @return
 */
private String getCitrixLicense(String requestJson) {
    String licenses = "";
    OutputStreamWriter out = null;
    InputStream is = null;
    long start = System.currentTimeMillis();
    try {
        // ws url
        URL url = new URL(map.get("address"));
        // ws connection
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestProperty("connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", " application/json");
        connection.setRequestMethod("POST");
        connection.connect();
        out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
        out.append(requestJson);
        out.flush();
        // response string
        is = connection.getInputStream();
        int length = is.available();
        if (0 < length) {
            long end = System.currentTimeMillis();
            System.out.println("?" + (end - start));
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            StringBuilder tempStr = new StringBuilder();
            String temp = "";
            while ((temp = reader.readLine()) != null) {
                tempStr.append(temp);
            }
            licenses = tempStr.toString(); // utf-8?
            System.out.println(licenses);
        }
    } catch (IOException e) {
        String eMsg = e.getMessage();
        if ("Read timed out".equals(eMsg) || "Connection timed out: connect".equals(eMsg)
                || "Software caused connection abort: recv failed".equals(eMsg)) {
            long end = System.currentTimeMillis();
            System.out.println(eMsg + (end - start));
            licenses = "TIMEOUT";
        }
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
            if (is != null) {
                is.close();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    System.out.println(licenses);
    return licenses;
}

From source file:com.msopentech.odatajclient.testservice.utils.XMLUtilities.java

@Override
public InputStream setChanges(final InputStream toBeChanged, final Map<String, InputStream> properties)
        throws Exception {
    XMLEventReader reader = getEventReader(toBeChanged);

    final ByteArrayOutputStream bos = new ByteArrayOutputStream();
    final XMLOutputFactory xof = XMLOutputFactory.newInstance();
    XMLEventWriter writer = xof.createXMLEventWriter(bos);

    // ---------------------------------
    // add property changes
    // ---------------------------------
    Map.Entry<Integer, XmlElement> propertyElement = getAtomElement(reader, writer, PROPERTIES, null, 0, 2, 3,
            false);/*from w w  w.java 2 s  . c  o  m*/

    writer.flush();

    ByteArrayOutputStream pbos = new ByteArrayOutputStream();
    OutputStreamWriter pwriter = new OutputStreamWriter(pbos);

    final XMLEventReader propertyReader = propertyElement.getValue().getContentReader();

    try {
        while (true) {
            final XmlElement property = getAtomElement(propertyReader, null, null);
            final String name = property.getStart().getName().getLocalPart();

            if (properties.containsKey(name)) {
                // replace
                final InputStream replacement = properties.get(name);
                properties.remove(property.getStart().getName().getLocalPart());
                pwriter.append(IOUtils.toString(replacement));
                IOUtils.closeQuietly(replacement);
            } else {
                pwriter.append(IOUtils.toString(property.toStream()));
            }
        }
    } catch (Exception ignore) {
        // end
    }

    for (Map.Entry<String, InputStream> remains : properties.entrySet()) {
        if (!remains.getKey().startsWith("[LINK]")) {
            pwriter.append(IOUtils.toString(remains.getValue()));
            IOUtils.closeQuietly(remains.getValue());
        }
    }

    pwriter.flush();
    pwriter.close();

    writer.add(propertyElement.getValue().getStart());
    writer.add(new XMLEventReaderWrapper(new ByteArrayInputStream(pbos.toByteArray())));
    writer.add(propertyElement.getValue().getEnd());

    IOUtils.closeQuietly(pbos);

    writer.add(reader);
    reader.close();
    writer.flush();
    writer.close();
    // ---------------------------------

    // ---------------------------------
    // add navigationm changes
    // ---------------------------------

    // remove existent links
    for (Map.Entry<String, InputStream> remains : properties.entrySet()) {

        if (remains.getKey().startsWith("[LINK]")) {
            reader = getEventReader(new ByteArrayInputStream(bos.toByteArray()));

            bos.reset();
            writer = xof.createXMLEventWriter(bos);

            try {
                final String linkName = remains.getKey().substring(remains.getKey().indexOf("]") + 1);

                getAtomElement(reader, writer, LINK, Collections.<Map.Entry<String, String>>singleton(
                        new SimpleEntry<String, String>("title", linkName)), 0, 2, 2, false);

                writer.add(reader);

            } catch (Exception ignore) {
                // ignore
            }

            writer.flush();
            writer.close();
        }
    }

    reader = getEventReader(new ByteArrayInputStream(bos.toByteArray()));

    bos.reset();
    writer = xof.createXMLEventWriter(bos);

    propertyElement = getAtomElement(reader, writer, CONTENT, null, 0, 2, 2, false);
    writer.flush();

    pbos.reset();
    pwriter = new OutputStreamWriter(pbos);

    for (Map.Entry<String, InputStream> remains : properties.entrySet()) {
        if (remains.getKey().startsWith("[LINK]")) {
            pwriter.append(IOUtils.toString(remains.getValue()));
            IOUtils.closeQuietly(remains.getValue());
        }
    }

    pwriter.flush();
    pwriter.close();

    writer.add(new XMLEventReaderWrapper(new ByteArrayInputStream(pbos.toByteArray())));
    IOUtils.closeQuietly(pbos);

    writer.add(propertyElement.getValue().getStart());
    writer.add(propertyElement.getValue().getContentReader());
    writer.add(propertyElement.getValue().getEnd());

    writer.add(reader);
    reader.close();
    writer.flush();
    writer.close();
    // ---------------------------------

    return new ByteArrayInputStream(bos.toByteArray());
}