Changeset 32941 in osm for applications/editors/josm


Ignore:
Timestamp:
2016-09-07T23:37:53+02:00 (8 years ago)
Author:
donvip
Message:

code cleanup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/tag2link/src/org/openstreetmap/josm/plugins/tag2link/action/OpenLinkAction.java

    r30717 r32941  
    1919
    2020import java.awt.event.ActionEvent;
    21 import java.io.FileOutputStream;
     21import java.io.File;
    2222import java.io.IOException;
    2323import java.io.InputStream;
    24 import java.io.OutputStreamWriter;
    25 import java.net.HttpURLConnection;
    26 import java.net.MalformedURLException;
    2724import java.net.URL;
     25import java.nio.charset.StandardCharsets;
     26import java.nio.file.Files;
    2827
    2928import org.openstreetmap.josm.Main;
     
    3231import org.openstreetmap.josm.plugins.tag2link.data.Link;
    3332import org.openstreetmap.josm.plugins.tag2link.data.LinkPost;
     33import org.openstreetmap.josm.tools.HttpClient;
    3434import org.openstreetmap.josm.tools.OpenBrowser;
    3535
     
    3737 * Action allowing to open a general link.
    3838 * @author Don-vip
    39  *
    4039 */
    4140@SuppressWarnings("serial")
     
    5857            try {
    5958                LinkPost lp = (LinkPost) link;
    60                 System.out.println("Sending POST request to "+lp.url);
    61                 HttpURLConnection conn = (HttpURLConnection) new URL(lp.url).openConnection();
    62                 conn.setDoOutput(true);
    63                 conn.setRequestMethod("POST");
    64                 for (String header : lp.headers.keySet()) {
    65                     conn.setRequestProperty(header, lp.headers.get(header));
    66                 }
    6759                String data = "";
    6860                for (String param : lp.params.keySet()) {
     
    7264                    data += param+"="+lp.params.get(param);
    7365                }
    74                 try (OutputStreamWriter osw = new OutputStreamWriter(conn.getOutputStream())) {
    75                     osw.write(data);
    76                     osw.flush();
    77                 }
    7866               
    7967                String filename = "output.pdf";// FIXME: should work for PDF files only (not even tested)
    80                 try (FileOutputStream fos = new FileOutputStream(filename);
    81                      InputStream is = conn.getInputStream()) {
    82                     byte[] buffer = new byte[2048];
    83                     int n = -1;
    84                     while ((n = is.read(buffer)) > -1) {
    85                         fos.write(buffer, 0, n);
    86                     }
     68                try (InputStream is = HttpClient.create(new URL(lp.url), "POST").setHeaders(lp.headers)
     69                        .setRequestBody(data.getBytes(StandardCharsets.UTF_8)).connect().getContent()) {
     70                    Files.copy(is, new File(filename).toPath());
    8771                }
    8872               
     
    9276                    Main.error(result);
    9377                }
    94                
    95             } catch (MalformedURLException ex) {
    96                 Main.error(ex);
    9778            } catch (IOException ex) {
    9879                Main.error(ex);
Note: See TracChangeset for help on using the changeset viewer.