Changeset 13458 in osm for applications/editors/josm/plugins/DirectUpload/src
- Timestamp:
- 2009-01-31T14:55:06+01:00 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/DirectUpload/src/org/openstreetmap/josm/plugins/DirectUpload/UploadDataGui.java
r13373 r13458 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; … … 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; … … 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"; … … 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) { … … 106 109 } 107 110 }); 108 109 JLabel directUploadLabel = new JLabel(tr("Direct Upload to OpenStreetMap"));110 111 111 112 publicCheckbox.setText(tr("Public")); … … 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("[&?/\\\\.,;]"," "); 171 172 OutputDisplay.setText(tr("Starting to upload selected file to openstreetmap.org")); 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); 173 180 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); … … 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);201 202 if ( returnCode!= 200) {203 boolean success = returnMsg.equals("OK"); 204 OutputDisplay.setText(success ? tr("GPX upload was successful") : returnMsg); 205 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) { … … 214 219 } 215 220 } 216 217 221 218 222 private boolean checkForErrors(String username, String password, String description, GpxData gpxData) { 219 223 String errors=""; … … 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 … … 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 … … 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();
Note:
See TracChangeset
for help on using the changeset viewer.