Ticket #1797: DirectUpload.patch

File DirectUpload.patch, 8.0 KB (added by xeen, 16 years ago)

The patch fixes the encoding issue and implements a minimal solution for #1876 (it shows a progress bar instead of hanging, but it doesn't actually report progress).

  • DirectUpload/src/org/openstreetmap/josm/plugins/DirectUpload/UploadDataGui.java

     
    1919import java.io.DataOutputStream;
    2020import java.io.IOException;
    2121import java.io.UnsupportedEncodingException;
     22import java.lang.String;
    2223import java.net.HttpURLConnection;
    2324import java.net.MalformedURLException;
    2425import java.net.URL;
     
    4647import org.openstreetmap.josm.gui.layer.Layer;
    4748import org.openstreetmap.josm.gui.layer.GpxLayer;
    4849import org.openstreetmap.josm.gui.MapView;
     50import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    4951import org.openstreetmap.josm.tools.Base64;
    5052import org.openstreetmap.josm.tools.GBC;
    5153import static org.openstreetmap.josm.tools.I18n.tr;
     
    5961    private JTextField descriptionField = new JTextField();
    6062    private JTextField tagsField = new JTextField();
    6163    private JCheckBox publicCheckbox = new JCheckBox();
     64    private JButton OkButton = new JButton();
    6265
    6366    public static final String API_VERSION = "0.5";
    6467    private static final String BOUNDARY = "----------------------------d10f7aa230e8";
     
    9295        JScrollPane jScrollPane1 = new JScrollPane();
    9396        jScrollPane1.setViewportView(OutputDisplay);
    9497
    95         JButton OkButton = new JButton(tr("Upload GPX track"));
     98        OkButton.setText(tr("Upload GPX track"));
    9699        OkButton.addActionListener(new java.awt.event.ActionListener() {
    97100            public void actionPerformed(java.awt.event.ActionEvent evt) {
    98101                OkButtonActionPerformed(evt);
     
    106109            }
    107110        });
    108111
    109         JLabel directUploadLabel = new JLabel(tr("Direct Upload to OpenStreetMap"));
    110 
    111112        publicCheckbox.setText(tr("Public"));
    112113        publicCheckbox.setToolTipText(tr("Selected makes your trace public in openstreetmap.org"));
    113114
     
    165166    public void upload(String username, String password, String description, String tags, Boolean isPublic, GpxData gpxData) throws IOException {
    166167        if(checkForErrors(username, password, description, gpxData))
    167168            return;
    168 
     169                               
     170                                OkButton.setEnabled(false);
     171                               
    169172        description = description.replaceAll("[&?/\\\\]"," ");
    170173        tags = tags.replaceAll("[&?/\\\\.,;]"," ");
     174       
     175        Main.pleaseWaitDlg.progress.setValue(0);
     176        Main.pleaseWaitDlg.setIndeterminate(true);
     177        Main.pleaseWaitDlg.currentAction.setText(tr("Connecting..."));
     178        // We don't support cancellation yet, so do not advertise it
     179        Main.pleaseWaitDlg.cancel.setEnabled(false);
    171180
    172         OutputDisplay.setText(tr("Starting to upload selected file to openstreetmap.org"));
    173 
    174181        try {
    175182            URL url = new URL("http://www.openstreetmap.org/api/" + API_VERSION + "/gpx/create");
    176             //System.err.println("url: " + url);
    177             //OutputDisplay.setText("Uploading in Progress");
    178183            HttpURLConnection connect = (HttpURLConnection) url.openConnection();
    179184            connect.setConnectTimeout(15000);
    180185            connect.setRequestMethod("POST");
     
    184189            connect.addRequestProperty("Connection", "close"); // counterpart of keep-alive
    185190            connect.addRequestProperty("Expect", "");
    186191            connect.connect();
    187             DataOutputStream out  = new DataOutputStream(new BufferedOutputStream(connect.getOutputStream()));
    188 
     192           
     193            Main.pleaseWaitDlg.currentAction.setText(tr("Uploading GPX track..."));
     194            DataOutputStream out  = new DataOutputStream(new BufferedOutputStream(connect.getOutputStream()));           
    189195            writeContentDispositionGpxData(out, "file", gpxData);
    190196            writeContentDisposition(out, "description", description);
    191             writeContentDisposition(out, "tags", (tags!=null && tags.length()>0) ? tags : "");
     197            writeContentDisposition(out, "tags", (tags != null && tags.length() > 0) ? tags : "");
    192198            writeContentDisposition(out, "public", isPublic ? "1" : "0");
    193 
    194199            out.writeBytes("--" + BOUNDARY + "--" + LINE_END);
    195200            out.flush();
    196 
    197             int returnCode = connect.getResponseCode();
     201           
    198202            String returnMsg = connect.getResponseMessage();
    199             //System.err.println(returnCode);
    200             OutputDisplay.setText(returnMsg);
     203            boolean success = returnMsg.equals("OK");
     204            OutputDisplay.setText(success ? tr("GPX upload was successful") : returnMsg);
    201205
    202             if (returnCode != 200) {
     206            if (connect.getResponseCode() != 200) {
    203207                if (connect.getHeaderField("Error") != null)
    204208                    returnMsg += "\n" + connect.getHeaderField("Error");
    205                 connect.disconnect();
    206209            }
    207210            out.close();
    208211            connect.disconnect();
    209 
     212           
     213            OkButton.setEnabled(!success);
     214           
    210215        } catch(UnsupportedEncodingException ignore) {
    211216        } catch (MalformedURLException e) {
    212217            OutputDisplay.setText(tr("Error while uploading"));
    213218            e.printStackTrace();
    214219        }
    215220    }
    216 
    217 
     221   
    218222    private boolean checkForErrors(String username, String password, String description, GpxData gpxData) {
    219223        String errors="";
    220224        if(description == null || description.length() == 0)
     
    244248    private void OkButtonActionPerformed(java.awt.event.ActionEvent evt) {
    245249        if(checkForGPXLayer()) return;
    246250
    247         //System.out.println(Descriptionfield);
    248         try {
    249             upload(Main.pref.get("osm-server.username"),
    250                    Main.pref.get("osm-server.password"),
    251                    descriptionField.getText(),
    252                    tagsField.getText(),
    253                    publicCheckbox.isSelected(),
    254                    ((GpxLayer)Main.map.mapView.getActiveLayer()).data
    255             );
    256         } catch (IOException ex) {
    257             Logger.getLogger(UploadDataGui.class.getName()).log(Level.SEVERE, null, ex);
    258         }
     251        PleaseWaitRunnable uploadTask = new PleaseWaitRunnable(tr("Uploading GPX Track")){
     252            @Override protected void realRun() throws IOException {
     253                  setAlwaysOnTop(false);
     254                  upload(Main.pref.get("osm-server.username"),
     255                         Main.pref.get("osm-server.password"),
     256                         descriptionField.getText(),
     257                         tagsField.getText(),
     258                         publicCheckbox.isSelected(),
     259                         ((GpxLayer)Main.map.mapView.getActiveLayer()).data
     260                  );
     261            }
     262            @Override protected void finish() {
     263                setAlwaysOnTop(true);
     264            }
     265            @Override protected void cancel() {
     266            }
     267        };
     268       
     269        Main.worker.execute(uploadTask);
    259270    }
    260271
    261272    private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
     
    266277        out.writeBytes("--" + BOUNDARY + LINE_END);
    267278        out.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"" + LINE_END);
    268279        out.writeBytes(LINE_END);
    269         out.writeBytes(value + LINE_END);
     280        // DataOutputStream's "writeUTF" method is unsuitable here because it adds two
     281        // char length bytes in front
     282        byte[] temp=value.getBytes("UTF-8");
     283        out.write(temp, 0, temp.length);
     284        out.writeBytes(LINE_END);
    270285    }
    271286
    272287    private void writeContentDispositionGpxData(DataOutputStream out, String name, GpxData gpxData) throws IOException {
     
    274289        out.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + datename +".gpx" + "\"" + LINE_END);
    275290        out.writeBytes("Content-Type: application/octet-stream" + LINE_END);
    276291        out.writeBytes(LINE_END);
    277 
    278         OutputDisplay.setText(tr("Transferring data to server"));
    279292        new GpxWriter(out).write(gpxData);
    280293        out.flush();
    281294        out.writeBytes(LINE_END);