Ticket #1797: DirectUpload.patch
File DirectUpload.patch, 8.0 KB (added by , 16 years ago) |
---|
-
DirectUpload/src/org/openstreetmap/josm/plugins/DirectUpload/UploadDataGui.java
19 19 import java.io.DataOutputStream; 20 20 import java.io.IOException; 21 21 import java.io.UnsupportedEncodingException; 22 import java.lang.String; 22 23 import java.net.HttpURLConnection; 23 24 import java.net.MalformedURLException; 24 25 import java.net.URL; … … 46 47 import org.openstreetmap.josm.gui.layer.Layer; 47 48 import org.openstreetmap.josm.gui.layer.GpxLayer; 48 49 import org.openstreetmap.josm.gui.MapView; 50 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 49 51 import org.openstreetmap.josm.tools.Base64; 50 52 import org.openstreetmap.josm.tools.GBC; 51 53 import static org.openstreetmap.josm.tools.I18n.tr; … … 59 61 private JTextField descriptionField = new JTextField(); 60 62 private JTextField tagsField = new JTextField(); 61 63 private JCheckBox publicCheckbox = new JCheckBox(); 64 private JButton OkButton = new JButton(); 62 65 63 66 public static final String API_VERSION = "0.5"; 64 67 private static final String BOUNDARY = "----------------------------d10f7aa230e8"; … … 92 95 JScrollPane jScrollPane1 = new JScrollPane(); 93 96 jScrollPane1.setViewportView(OutputDisplay); 94 97 95 JButton OkButton = new JButton(tr("Upload GPX track"));98 OkButton.setText(tr("Upload GPX track")); 96 99 OkButton.addActionListener(new java.awt.event.ActionListener() { 97 100 public void actionPerformed(java.awt.event.ActionEvent evt) { 98 101 OkButtonActionPerformed(evt); … … 106 109 } 107 110 }); 108 111 109 JLabel directUploadLabel = new JLabel(tr("Direct Upload to OpenStreetMap"));110 111 112 publicCheckbox.setText(tr("Public")); 112 113 publicCheckbox.setToolTipText(tr("Selected makes your trace public in openstreetmap.org")); 113 114 … … 165 166 public void upload(String username, String password, String description, String tags, Boolean isPublic, GpxData gpxData) throws IOException { 166 167 if(checkForErrors(username, password, description, gpxData)) 167 168 return; 168 169 170 OkButton.setEnabled(false); 171 169 172 description = description.replaceAll("[&?/\\\\]"," "); 170 173 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); 171 180 172 OutputDisplay.setText(tr("Starting to upload selected file to openstreetmap.org"));173 174 181 try { 175 182 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");178 183 HttpURLConnection connect = (HttpURLConnection) url.openConnection(); 179 184 connect.setConnectTimeout(15000); 180 185 connect.setRequestMethod("POST"); … … 184 189 connect.addRequestProperty("Connection", "close"); // counterpart of keep-alive 185 190 connect.addRequestProperty("Expect", ""); 186 191 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())); 189 195 writeContentDispositionGpxData(out, "file", gpxData); 190 196 writeContentDisposition(out, "description", description); 191 writeContentDisposition(out, "tags", (tags !=null && tags.length()>0) ? tags : "");197 writeContentDisposition(out, "tags", (tags != null && tags.length() > 0) ? tags : ""); 192 198 writeContentDisposition(out, "public", isPublic ? "1" : "0"); 193 194 199 out.writeBytes("--" + BOUNDARY + "--" + LINE_END); 195 200 out.flush(); 196 197 int returnCode = connect.getResponseCode(); 201 198 202 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); 201 205 202 if ( returnCode!= 200) {206 if (connect.getResponseCode() != 200) { 203 207 if (connect.getHeaderField("Error") != null) 204 208 returnMsg += "\n" + connect.getHeaderField("Error"); 205 connect.disconnect();206 209 } 207 210 out.close(); 208 211 connect.disconnect(); 209 212 213 OkButton.setEnabled(!success); 214 210 215 } catch(UnsupportedEncodingException ignore) { 211 216 } catch (MalformedURLException e) { 212 217 OutputDisplay.setText(tr("Error while uploading")); 213 218 e.printStackTrace(); 214 219 } 215 220 } 216 217 221 218 222 private boolean checkForErrors(String username, String password, String description, GpxData gpxData) { 219 223 String errors=""; 220 224 if(description == null || description.length() == 0) … … 244 248 private void OkButtonActionPerformed(java.awt.event.ActionEvent evt) { 245 249 if(checkForGPXLayer()) return; 246 250 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); 259 270 } 260 271 261 272 private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) { … … 266 277 out.writeBytes("--" + BOUNDARY + LINE_END); 267 278 out.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"" + LINE_END); 268 279 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); 270 285 } 271 286 272 287 private void writeContentDispositionGpxData(DataOutputStream out, String name, GpxData gpxData) throws IOException { … … 274 289 out.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + datename +".gpx" + "\"" + LINE_END); 275 290 out.writeBytes("Content-Type: application/octet-stream" + LINE_END); 276 291 out.writeBytes(LINE_END); 277 278 OutputDisplay.setText(tr("Transferring data to server"));279 292 new GpxWriter(out).write(gpxData); 280 293 out.flush(); 281 294 out.writeBytes(LINE_END);