Example usage for java.io OutputStreamWriter write

List of usage examples for java.io OutputStreamWriter write

Introduction

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

Prototype

public void write(int c) throws IOException 

Source Link

Document

Writes a single character.

Usage

From source file:org.kite9.diagram.server.AbstractKite9Controller.java

protected void sendErrorFormBack(OutputStream os, String formName) throws IOException {
    OutputStreamWriter osw = new OutputStreamWriter(os);
    osw.write("form=" + formName);
    osw.close();/*from w  ww  .  j av a 2 s . c om*/
}

From source file:com.hqme.cm.cache.StreamingServer.java

public void stopServer() {
    UntenCacheService.debugLog(sTag, "stopServer");
    isStopping = true;/*from ww w  . j  a va2s  . c  o m*/
    try {
        URL term = new URL("http://localhost:" + serverPortNumber + "/");
        URLConnection conn = term.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write("GET /favicon.ico HTTP/1.1");
        out.close();
    } catch (Throwable fault) {
        // UntenCacheService.debugLog(sTag, "stopServer", fault);
    }
}

From source file:biblivre3.cataloging.bibliographic.BiblioBO.java

public MemoryFileDTO createFileLabelsTXT(final Collection<LabelDTO> labels) {
    final MemoryFileDTO file = new MemoryFileDTO();
    file.setFileName(new Date().getTime() + ".txt");
    try {/*from  w  ww.  java2s  .  co m*/
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final OutputStreamWriter writer = new OutputStreamWriter(baos, "UTF-8");
        PrintWriter out = new PrintWriter(writer, true);
        for (LabelDTO ldto : labels) {
            out.println("Tombo");
            out.println(ldto.getAssetHolding());
            writer.write(ApplicationConstants.LINE_BREAK);
            out.println("Autor");
            out.println(ldto.getAuthor());
            writer.write(ApplicationConstants.LINE_BREAK);
            out.println("Titulo");
            out.println(ldto.getTitle());
            writer.write(ApplicationConstants.LINE_BREAK);
            out.println("Loc. A");
            out.println(ldto.getLocationA());
            writer.write(ApplicationConstants.LINE_BREAK);
            out.println("Loc. B");
            out.println(ldto.getLocationB());
            writer.write(ApplicationConstants.LINE_BREAK);
            out.println("Loc. C");
            out.println(ldto.getLocationC());
            writer.write(ApplicationConstants.LINE_BREAK);
            out.println("Loc. D");
            out.println(ldto.getLocationD());
            out.println("---------------------------------|");
        }
        writer.flush();
        writer.close();
        file.setFileData(baos.toByteArray());
    } catch (Exception e) {
        e.getMessage();
    }
    return file;
}

From source file:com.networknt.light.server.LoadPageMojo.java

private void writeSqlToOutputFile() {
    OutputStreamWriter out = null;
    try {//from   w  w w.  j  a v a2 s .  co  m
        final StringBuffer path = new StringBuffer();
        path.append(outputDirectory);
        path.append(System.getProperty("file.separator"));
        path.append("server.sql");

        final FileOutputStream fos = new FileOutputStream(path.toString());
        out = new OutputStreamWriter(fos, encoding);
        out.write(sqlString);

    } catch (final IOException e) {
        getLog().error(e.getMessage());
    } finally {
        if (null != out) {
            try {
                out.close();
            } catch (final IOException e) {
                getLog().error(e.getMessage());
            }
        }
    }
}

From source file:fi.cosky.sdk.API.java

private <T extends BaseData> T sendRequestWithAddedHeaders(Verb verb, String url, Class<T> tClass,
        Object object, HashMap<String, String> headers) throws IOException {
    URL serverAddress;//w  w  w . ja  v  a  2  s.  c o m
    HttpURLConnection connection;
    BufferedReader br;
    String result = "";
    try {
        serverAddress = new URL(url);
        connection = (HttpURLConnection) serverAddress.openConnection();
        connection.setInstanceFollowRedirects(false);
        boolean doOutput = doOutput(verb);
        connection.setDoOutput(doOutput);
        connection.setRequestMethod(method(verb));
        connection.setRequestProperty("Authorization", headers.get("authorization"));
        connection.addRequestProperty("Accept", "application/json");

        if (doOutput) {
            connection.addRequestProperty("Content-Length", "0");
            OutputStreamWriter os = new OutputStreamWriter(connection.getOutputStream());
            os.write("");
            os.flush();
            os.close();
        }
        connection.connect();

        if (connection.getResponseCode() == HttpURLConnection.HTTP_SEE_OTHER
                || connection.getResponseCode() == HttpURLConnection.HTTP_CREATED) {
            Link location = parseLocationLinkFromString(connection.getHeaderField("Location"));
            Link l = new Link("self", "/tokens", "GET", "", true);
            ArrayList<Link> links = new ArrayList<Link>();
            links.add(l);
            links.add(location);
            ResponseData data = new ResponseData();
            data.setLocation(location);
            data.setLinks(links);
            return (T) data;
        }

        if (connection.getResponseCode() == HttpURLConnection.HTTP_UNAUTHORIZED) {
            System.out.println("Authentication expired: " + connection.getResponseMessage());
            if (retry && this.tokenData != null) {
                retry = false;
                this.tokenData = null;
                if (authenticate()) {
                    System.out.println(
                            "Reauthentication success, will continue with " + verb + " request on " + url);
                    return sendRequestWithAddedHeaders(verb, url, tClass, object, headers);
                }
            } else
                throw new IOException(
                        "Tried to reauthenticate but failed, please check the credentials and status of NFleet-API");
        }

        if (connection.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST
                && connection.getResponseCode() < HttpURLConnection.HTTP_INTERNAL_ERROR) {
            System.out.println("ErrorCode: " + connection.getResponseCode() + " "
                    + connection.getResponseMessage() + " " + url + ", verb: " + verb);

            String errorString = readErrorStreamAndCloseConnection(connection);
            throw (NFleetRequestException) gson.fromJson(errorString, NFleetRequestException.class);
        } else if (connection.getResponseCode() >= HttpURLConnection.HTTP_INTERNAL_ERROR) {
            if (retry) {
                System.out.println("Server responded with internal server error, trying again in "
                        + RETRY_WAIT_TIME + " msec.");
                try {
                    retry = false;
                    Thread.sleep(RETRY_WAIT_TIME);
                    return sendRequestWithAddedHeaders(verb, url, tClass, object, headers);
                } catch (InterruptedException e) {

                }
            } else {
                System.out.println("Server responded with internal server error, please contact dev@nfleet.fi");
            }

            String errorString = readErrorStreamAndCloseConnection(connection);
            throw new IOException(errorString);
        }

        result = readDataFromConnection(connection);
    } catch (MalformedURLException e) {
        throw e;
    } catch (ProtocolException e) {
        throw e;
    } catch (UnsupportedEncodingException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    }
    return (T) gson.fromJson(result, tClass);
}

From source file:com.networknt.light.rule.LoadRuleMojo.java

private void writeSqlToOutputFile() {
    OutputStreamWriter out = null;
    try {//from w  w  w .  java2 s .c  om
        final StringBuffer path = new StringBuffer();
        path.append(outputDirectory);
        path.append(System.getProperty("file.separator"));
        path.append("rule.sql");

        final FileOutputStream fos = new FileOutputStream(path.toString());
        out = new OutputStreamWriter(fos, encoding);
        out.write(sqlString);

    } catch (final IOException e) {
        getLog().error(e.getMessage());
    } finally {
        if (null != out) {
            try {
                out.close();
            } catch (final IOException e) {
                getLog().error(e.getMessage());
            }
        }
    }
}

From source file:com.mycompany.grupo6ti.GenericResource.java

@POST
@Produces("application/json")
@Path("/cancelarFactura/{id}/{motivo}")
public String anularFactura(@PathParam("id") String id, @PathParam("motivo") String motivo) {
    try {/*from w w w  .  j av  a 2s. co  m*/
        URL url = new URL("http://localhost:85/cancel/");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream is;
        conn.setRequestProperty("Accept-Charset", "UTF-8");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setUseCaches(true);
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);

        String idJson = "{\n" + "  \"id\": \"" + id + "\",\n" + "  \"motivo\": \"" + motivo + "\"\n" + "}";
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write(idJson);
        out.flush();
        out.close();

        if (conn.getResponseCode() >= 400) {
            is = conn.getErrorStream();
        } else {
            is = conn.getInputStream();
        }
        String result2 = "";
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = rd.readLine()) != null) {
            result2 += line;
        }
        rd.close();
        return result2;

    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return ("[\"Test\", \"Funcionando Bien\"]");

}

From source file:com.mycompany.grupo6ti.GenericResource.java

@PUT
@Produces("application/json")
@Path("emitirFactura/{id_oC}")
public String emitirFactura(@PathParam("id_oC") String id_oC) {
    try {//from w w  w  . ja  v a  2s.com
        URL url = new URL("http://localhost:85/");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream is;
        conn.setRequestProperty("Accept-Charset", "UTF-8");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setUseCaches(true);
        conn.setRequestMethod("PUT");
        conn.setDoOutput(true);
        conn.setDoInput(true);

        String idJson = "{\n" + "  \"oc\": \"" + id_oC + "\"\n" + "}";
        //String idJson2 = "[\"Test\", \"Funcionando Bien\"]";
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write(idJson);
        out.flush();
        out.close();

        if (conn.getResponseCode() >= 400) {
            is = conn.getErrorStream();
        } else {
            is = conn.getInputStream();
        }
        String result2 = "";
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = rd.readLine()) != null) {
            result2 += line;
        }
        rd.close();
        return result2;

    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return ("[\"Test\", \"Funcionando Bien\"]");

}

From source file:com.mycompany.grupo6ti.GenericResource.java

@POST
@Produces("application/json")

@Path("/pagarFactura/{id}")
public String pagarFactura(@PathParam("id") String id) {
    try {/*from  ww w  .  j a va 2s  .  c om*/
        URL url = new URL("http://localhost:85/pay");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream is;
        conn.setRequestProperty("Accept-Charset", "UTF-8");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setUseCaches(true);

        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);

        String idJson = "{\n" + "  \"id\": \"" + id + "\"\n" + "}";
        //String idJson2 = "[\"Test\", \"Funcionando Bien\"]";
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write(idJson);
        out.flush();
        out.close();

        if (conn.getResponseCode() >= 400) {
            is = conn.getErrorStream();
        } else {
            is = conn.getInputStream();
        }
        String result2 = "";
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = rd.readLine()) != null) {
            result2 += line;
        }
        rd.close();
        return result2;

    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return ("[\"Test\", \"Funcionando Bien\"]");

}

From source file:com.mycompany.grupo6ti.GenericResource.java

@POST
@Produces("application/json")
@Path("/rechazarFactura/{id}/{motivo}")
public String rechazarFactura(@PathParam("id") String id, @PathParam("motivo") String motivo) {
    try {//from   w ww . ja  v a  2s . c o m
        URL url = new URL("http://localhost:85/reject/");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        InputStream is;
        conn.setRequestProperty("Accept-Charset", "UTF-8");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setUseCaches(true);
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);
        conn.setDoInput(true);

        String idJson = "{\n" + "  \"id\": \"" + id + "\",\n" + "  \"motivo\": \"" + motivo + "\"\n" + "}";
        //String idJson2 = "[\"Test\", \"Funcionando Bien\"]";
        OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
        out.write(idJson);
        out.flush();
        out.close();

        if (conn.getResponseCode() >= 400) {
            is = conn.getErrorStream();
        } else {
            is = conn.getInputStream();
        }
        String result2 = "";
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        while ((line = rd.readLine()) != null) {
            result2 += line;
        }
        rd.close();
        return result2;

    } catch (IOException e) {
        e.printStackTrace();
        return ("[\"Test\", \"Funcionando Bien1\"]");
    } catch (Exception e) {
        e.printStackTrace();
        return ("[\"Test\", \"Funcionando Bien2\"]");
    }

    //return ("[\"Test\", \"Funcionando Bien\"]");

}