Ticket #1635: ImproveDirectUploadPlugin.patch
File ImproveDirectUploadPlugin.patch, 21.6 KB (added by , 17 years ago) |
---|
-
DirectUpload/src/org/openstreetmap/josm/plugins/DirectUpload/UploadDataGui.java
7 7 */ 8 8 9 9 package org.openstreetmap.josm.plugins.DirectUpload; 10 import java.awt.event.ItemEvent;11 import static org.openstreetmap.josm.tools.I18n.tr;12 10 11 import java.awt.BorderLayout; 12 import java.awt.Container; 13 import java.awt.Dimension; 14 import java.awt.event.ItemEvent; 15 import java.awt.GridBagLayout; 16 import java.awt.GridBagConstraints; 17 import java.awt.Toolkit; 13 18 import java.io.BufferedOutputStream; 14 19 import java.io.DataOutputStream; 15 20 import java.io.IOException; … … 22 27 import java.util.Date; 23 28 import java.util.logging.Level; 24 29 import java.util.logging.Logger; 30 31 import javax.swing.BorderFactory; 32 import javax.swing.Box; 33 import javax.swing.JButton; 34 import javax.swing.JCheckBox; 35 import javax.swing.JLabel; 25 36 import javax.swing.JOptionPane; 37 import javax.swing.JPanel; 38 import javax.swing.JScrollPane; 39 import javax.swing.JTextArea; 26 40 import javax.swing.JTextField; 27 import java.awt.Container; 28 import java.awt.GridBagLayout; 29 import java.awt.GridBagConstraints; 41 import javax.swing.UIManager; 42 30 43 import org.openstreetmap.josm.Main; 31 44 import org.openstreetmap.josm.data.gpx.GpxData; 32 45 import org.openstreetmap.josm.io.GpxWriter; 33 46 import org.openstreetmap.josm.gui.layer.Layer; 34 47 import org.openstreetmap.josm.gui.layer.GpxLayer; 35 48 import org.openstreetmap.josm.gui.MapView; 49 import org.openstreetmap.josm.tools.Base64; 50 import org.openstreetmap.josm.tools.GBC; 51 import static org.openstreetmap.josm.tools.I18n.tr; 36 52 37 53 /** 38 54 * 39 55 * @author subhodip 40 56 */ 41 57 public class UploadDataGui extends javax.swing.JFrame { 58 private JTextArea OutputDisplay = new JTextArea(); 59 private JTextField descriptionField = new JTextField(); 60 private JTextField tagsField = new JTextField(); 61 private JCheckBox publicCheckbox = new JCheckBox(); 62 63 public static final String API_VERSION = "0.5"; 64 private static final String BOUNDARY = "----------------------------d10f7aa230e8"; 65 private static final String LINE_END = "\r\n"; 66 67 private String datename = new SimpleDateFormat("yyMMddHHmmss").format(new Date()); 42 68 43 String tagging;44 String Descriptionfield;45 String urlDescription;46 String urlTags;47 String UserName;48 String PassWord;49 //GpxData gpxData;50 51 public static final String API_VERSION = "0.5";52 private static final String BASE64_ENC = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";53 private static final String BOUNDARY = "----------------------------d10f7aa230e8";54 private static final String LINE_END = "\r\n";55 56 boolean dfield;57 boolean taggy;58 boolean choosy;59 boolean user;60 boolean pass;61 boolean publicheck;62 63 DateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");64 Date date = new Date();65 String datename = dateFormat.format(date);66 67 69 /** Creates new form UploadDataGui */ 68 70 public UploadDataGui() { 71 setTitle(tr("Upload Traces")); 69 72 initComponents(); 70 73 } 71 72 73 /** This method is called from within the constructor to 74 * initialize the form. 75 * WARNING: Do NOT modify this code. The content of this method is 76 * always regenerated by the Form Editor. 77 */ 78 @SuppressWarnings("unchecked") 79 // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents 74 80 75 private void initComponents() { 81 82 jScrollPane1 = new javax.swing.JScrollPane();83 OutputDisplay = new javax.swing.JTextArea();84 OkButton = new javax.swing.JButton();85 CancelButton = new javax.swing.JButton();86 jLabel2 = new javax.swing.JLabel();87 PublicTrace = new javax.swing.JCheckBox();88 jLabel1 = new javax.swing.JLabel();89 descriptionfield = new javax.swing.JTextField();90 jLabel3 = new javax.swing.JLabel();91 tagfield = new javax.swing.JTextField();92 93 76 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 94 77 setAlwaysOnTop(true); 95 96 OutputDisplay.setColumns(20); 97 OutputDisplay.setRows(5); 78 setPreferredSize(new Dimension(350,200)); 79 80 // Display Center Screen 81 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 82 Dimension labelSize = getPreferredSize(); 83 setLocation(screenSize.width / 2 - (labelSize.width / 2), screenSize.height / 2 - (labelSize.height / 2)); 84 85 OutputDisplay.setBackground(UIManager.getColor("Panel.background")); 86 OutputDisplay.setBorder(BorderFactory.createEmptyBorder(0,0,0,0)); 87 OutputDisplay.setEditable(false); 88 OutputDisplay.setFont(new JLabel().getFont()); 89 OutputDisplay.setLineWrap(true); 90 OutputDisplay.setWrapStyleWord(true); 91 92 JScrollPane jScrollPane1 = new JScrollPane(); 98 93 jScrollPane1.setViewportView(OutputDisplay); 99 100 OkButton.setText("Ok");94 95 JButton OkButton = new JButton(tr("Upload GPX track")); 101 96 OkButton.addActionListener(new java.awt.event.ActionListener() { 102 97 public void actionPerformed(java.awt.event.ActionEvent evt) { 103 98 OkButtonActionPerformed(evt); 104 99 } 105 100 }); 106 107 CancelButton.setText("Cancel");101 102 JButton CancelButton = new JButton(tr("Cancel")); 108 103 CancelButton.addActionListener(new java.awt.event.ActionListener() { 109 104 public void actionPerformed(java.awt.event.ActionEvent evt) { 110 105 CancelButtonActionPerformed(evt); 111 106 } 112 107 }); 108 109 JLabel directUploadLabel = new JLabel(tr("Direct Upload to OpenStreetMap")); 110 111 publicCheckbox.setText(tr("Public")); 112 publicCheckbox.setToolTipText(tr("Selected makes your trace public in openstreetmap.org")); 113 114 JLabel descriptionLabel = new JLabel(tr("Description")); 115 descriptionField.setToolTipText("Please enter Description about your trace."); 113 116 114 jLabel2.setFont(new java.awt.Font("DejaVu Sans", 1, 14)); 115 jLabel2.setText("Direct Upload to OpenStreetMap"); 117 JLabel tagsLabel = new JLabel(tr("Tags")); 118 tagsField.setToolTipText("Please enter tags about your trace."); 119 120 JPanel p = new JPanel(); 121 p.setLayout(new GridBagLayout()); 116 122 117 PublicTrace.setText("Public"); 118 PublicTrace.setToolTipText("Selected makes your trace public in openstreetmap.org"); 119 PublicTrace.addItemListener(new java.awt.event.ItemListener() { 120 public void itemStateChanged(java.awt.event.ItemEvent evt) { 121 PublicTraceItemStateChanged(evt); 122 } 123 }); 123 p.add(OutputDisplay, GBC.eol().fill()); 124 124 125 jLabel1.setText("Description"); 125 p.add(tagsLabel, GBC.std().insets(0,10,0,0)); 126 p.add(tagsField, GBC.eol().fill(GBC.HORIZONTAL)); 127 128 p.add(descriptionLabel, GBC.std().insets(0,10,0,0)); 129 p.add(descriptionField, GBC.eol().fill(GBC.HORIZONTAL)); 126 130 127 descriptionfield.setToolTipText("Please enter Description about your trace."); 128 descriptionfield.addFocusListener(new java.awt.event.FocusAdapter() { 129 public void focusLost(java.awt.event.FocusEvent evt) { 130 descriptionfieldFocusLost(evt); 131 } 132 }); 131 p.add(publicCheckbox, GBC.eol()); 133 132 134 jLabel3.setText("Tags"); 135 136 tagfield.setToolTipText("Please enter tags about your trace."); 137 tagfield.addFocusListener(new java.awt.event.FocusAdapter() { 138 public void focusLost(java.awt.event.FocusEvent evt) { 139 tagfieldFocusLost(evt); 140 } 141 }); 142 143 144 145 Container contentPane = getContentPane(); 146 contentPane.setLayout(new GridBagLayout()); 147 GridBagConstraints c = new GridBagConstraints(); 148 149 c.gridx = 0; 150 c.gridy = 0; 151 c.gridwidth = 2; 152 c.fill = 1; 153 contentPane.add(OutputDisplay, c); 154 155 c.gridwidth = 1; 156 c.gridy = 1; 157 contentPane.add(jLabel3, c); 158 c.gridx = 1; 159 contentPane.add(tagfield, c); 160 161 c.gridy = 2; c.gridx = 0; 162 contentPane.add(jLabel1, c); 163 c.gridx = 1; 164 contentPane.add(descriptionfield, c); 165 166 c.gridy = 3; c.gridx = 0; 167 contentPane.add(jLabel2, c); 168 c.gridx = 1; 169 contentPane.add(PublicTrace, c); 170 171 c.gridy = 4; c.gridx = 0; 172 contentPane.add(CancelButton, c); 173 c.gridx = 1; 174 contentPane.add(OkButton, c); 175 133 p.add(CancelButton, GBC.std()); 134 p.add(OkButton, GBC.eol().fill(GBC.HORIZONTAL)); 135 136 getContentPane().setLayout(new GridBagLayout()); 137 getContentPane().add(p, GBC.eol().insets(10,10,10,10).fill()); 138 176 139 pack(); 177 }// </editor-fold>//GEN-END:initComponents 178 179 180 181 public void upload(String username, String password, String Descriptionfield, String tagging , GpxData gpxData ) throws IOException { 182 OutputDisplay.setText("Starting to upload selected file to openstreetmap.org"); 183 184 try { 185 186 urlDescription = Descriptionfield.replaceAll("\\.;&?,/","_"); 187 if (urlDescription == null || urlDescription.length() == 0) { 188 OutputDisplay.setText("No description provided .Please provide some description . For the time being ignore the exception error "); 189 140 141 // If no GPX layer is selected, select one for the user if there is only one GPX layer 142 if(Main.map != null && Main.map.mapView != null) { 143 MapView mv=Main.map.mapView; 144 if(!(mv.getActiveLayer() instanceof GpxLayer)) { 145 Layer lastLayer=null; 146 int layerCount=0; 147 for (Layer l : mv.getAllLayers()) { 148 if(l instanceof GpxLayer) { 149 lastLayer = l; 150 layerCount++; 151 } 152 } 153 if(layerCount == 1) mv.setActiveLayer(lastLayer); 154 } 155 156 if(mv.getActiveLayer() instanceof GpxLayer) { 157 GpxData data=((GpxLayer)Main.map.mapView.getActiveLayer()).data; 158 descriptionField.setText(data.storageFile.getName().replaceAll("[&?/\\\\]"," ").replaceAll("(\\.[^.]*)$","")); 159 } 190 160 } 191 urlTags = tagging.replaceAll("\\\\.;&?,/","_"); 161 162 boolean x=checkForGPXLayer(); 163 } 192 164 165 public void upload(String username, String password, String description, String tags, Boolean isPublic, GpxData gpxData) throws IOException { 166 if(checkForErrors(username, password, description, gpxData)) 167 return; 168 169 description = description.replaceAll("[&?/\\\\]"," "); 170 tags = tags.replaceAll("[&?/\\\\.,;]"," "); 171 172 OutputDisplay.setText(tr("Starting to upload selected file to openstreetmap.org")); 193 173 194 URL url = new URL("http://www.openstreetmap.org/api/" + API_VERSION + "/gpx/create"); 195 System.err.println("url: " + url); 196 OutputDisplay.setText("Uploading in Progress"); 197 HttpURLConnection connect = (HttpURLConnection) url.openConnection(); 198 connect.setConnectTimeout(15000); 199 connect.setRequestMethod("POST"); 200 connect.setDoOutput(true); 201 connect.addRequestProperty("Authorization", "Basic "+encodeBase64(username+":"+password)); 202 connect.addRequestProperty("Content-Type", "multipart/form-data; boundary="+BOUNDARY); 203 connect.addRequestProperty("Connection", "close"); // counterpart of keep-alive 204 connect.addRequestProperty("Expect", ""); 205 connect.connect(); 206 DataOutputStream out = new DataOutputStream(new BufferedOutputStream(connect.getOutputStream())); 207 writeContentDispositionGpxData(out, "file", gpxData); 208 writeContentDisposition(out, "description", urlDescription); 209 writeContentDisposition(out, "tags", urlTags); 210 if(publicheck) { 211 System.out.println(publicheck); 212 writeContentDisposition(out, "public", "1"); 213 } else { 214 writeContentDisposition(out, "public", "0"); 215 } 216 out.writeBytes("--" + BOUNDARY + "--" + LINE_END); 217 out.flush(); 174 try { 175 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 HttpURLConnection connect = (HttpURLConnection) url.openConnection(); 179 connect.setConnectTimeout(15000); 180 connect.setRequestMethod("POST"); 181 connect.setDoOutput(true); 182 connect.addRequestProperty("Authorization", "Basic " + Base64.encode(username + ":" + password)); 183 connect.addRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); 184 connect.addRequestProperty("Connection", "close"); // counterpart of keep-alive 185 connect.addRequestProperty("Expect", ""); 186 connect.connect(); 187 DataOutputStream out = new DataOutputStream(new BufferedOutputStream(connect.getOutputStream())); 188 189 writeContentDispositionGpxData(out, "file", gpxData); 190 writeContentDisposition(out, "description", description); 191 writeContentDisposition(out, "tags", (tags!=null && tags.length()>0) ? tags : ""); 192 writeContentDisposition(out, "public", isPublic ? "1" : "0"); 193 194 out.writeBytes("--" + BOUNDARY + "--" + LINE_END); 195 out.flush(); 218 196 219 int returnCode = connect.getResponseCode();197 int returnCode = connect.getResponseCode(); 220 198 String returnMsg = connect.getResponseMessage(); 221 System.err.println(returnCode);199 //System.err.println(returnCode); 222 200 OutputDisplay.setText(returnMsg); 223 if (returnCode != 200) { 224 if (connect.getHeaderField("Error") != null) 225 returnMsg += "\n" + connect.getHeaderField("Error"); 201 202 if (returnCode != 200) { 203 if (connect.getHeaderField("Error") != null) 204 returnMsg += "\n" + connect.getHeaderField("Error"); 205 connect.disconnect(); 206 } 207 out.close(); 226 208 connect.disconnect(); 227 }228 out.close();229 connect.disconnect();230 209 231 210 } catch(UnsupportedEncodingException ignore) { 232 211 } catch (MalformedURLException e) { 233 OutputDisplay.setText("Cant Upload ."); 234 e.printStackTrace(); 212 OutputDisplay.setText(tr("Error while uploading")); 213 e.printStackTrace(); 214 } 235 215 } 236 216 237 } 238 239 240 241 public static String getPreferencesDir() { 242 if (System.getenv("APPDATA") != null) 243 return System.getenv("APPDATA")+"/JOSM/"; 244 return System.getProperty("user.home")+"/.josm/"; 217 public static String getPreferencesDir() { 218 if (System.getenv("APPDATA") != null) 219 return System.getenv("APPDATA")+"/JOSM/"; 220 return System.getProperty("user.home")+"/.josm/"; 245 221 } 246 247 private void OkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OkButtonActionPerformed 248 GpxData gpxData; 249 250 UserName = Main.pref.get("osm-server.username"); 251 PassWord = Main.pref.get("osm-server.password"); 252 253 if(Main.map == null || Main.map.mapView == null ||Main.map.mapView.getActiveLayer() == null || 254 !(Main.map.mapView.getActiveLayer() instanceof GpxLayer)){ 255 JOptionPane.showMessageDialog(Main.parent, 256 tr("No GPX layer selected. Cannot upload a trace.") 257 ); 258 return; 222 223 private boolean checkForErrors(String username, String password, String description, GpxData gpxData) { 224 String errors=""; 225 if(description == null || description.length() == 0) 226 errors += tr("No description provided. Please provide some description."); 227 228 if(gpxData == null) 229 errors += tr("No GPX layer selected. Cannot upload a trace."); 230 231 if(username == null || username.length()==0) 232 errors += tr("No username provided."); 233 234 if(password == null || password.length()==0) 235 errors += tr("No password provided."); 236 237 OutputDisplay.setText(errors); 238 return errors.length() > 0; 239 } 240 241 private boolean checkForGPXLayer() { 242 if(Main.map == null || Main.map.mapView == null || Main.map.mapView.getActiveLayer() == null || !(Main.map.mapView.getActiveLayer() instanceof GpxLayer)) { 243 OutputDisplay.setText(tr("No GPX layer selected. Cannot upload a trace.")); 244 return true; 259 245 } 260 gpxData = ((GpxLayer)Main.map.mapView.getActiveLayer()).data; 261 System.out.println(Descriptionfield); 246 return false; 247 } 248 249 private void OkButtonActionPerformed(java.awt.event.ActionEvent evt) { 250 if(checkForGPXLayer()) return; 251 252 //System.out.println(Descriptionfield); 262 253 try { 263 upload(UserName,PassWord,Descriptionfield ,tagging ,gpxData) ; 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 ); 264 261 } catch (IOException ex) { 265 Logger.getLogger(UploadDataGui.class.getName()).log(Level.SEVERE, null, ex); 262 Logger.getLogger(UploadDataGui.class.getName()).log(Level.SEVERE, null, ex); 263 } 266 264 } 267 265 268 }//GEN-LAST:event_OkButtonActionPerformed 269 270 private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed 266 private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) { 271 267 dispose(); 272 }//GEN-LAST:event_CancelButtonActionPerformed273 274 private void PublicTraceItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_PublicTraceItemStateChanged275 276 if (evt.getStateChange() == ItemEvent.SELECTED) {277 publicheck = true;278 279 268 } 280 }//GEN-LAST:event_PublicTraceItemStateChanged281 269 282 private void descriptionfieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_descriptionfieldFocusLost283 284 JTextField Descsel = (JTextField)evt.getSource();285 Descriptionfield = Descsel.getText();286 287 }//GEN-LAST:event_descriptionfieldFocusLost288 289 private void tagfieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tagfieldFocusLost290 291 JTextField Tagsel = (JTextField)evt.getSource();292 tagging = Tagsel.getText();293 294 }//GEN-LAST:event_tagfieldFocusLost295 296 297 270 private void writeContentDisposition(DataOutputStream out, String name, String value) throws IOException { 298 271 out.writeBytes("--" + BOUNDARY + LINE_END); 299 272 out.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"" + LINE_END); 300 273 out.writeBytes(LINE_END); 301 274 out.writeBytes(value + LINE_END); 302 303 275 } 304 276 305 private void writeContentDispositionGpxData(DataOutputStream out, String name, GpxData gpxData ) throws IOException { 306 277 private void writeContentDispositionGpxData(DataOutputStream out, String name, GpxData gpxData) throws IOException { 307 278 out.writeBytes("--" + BOUNDARY + LINE_END); 308 279 out.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + datename +".gpx" + "\"" + LINE_END); 309 //out.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + gpxData.storageFile.getName() + "\"" + LINE_END);310 280 out.writeBytes("Content-Type: application/octet-stream" + LINE_END); 311 281 out.writeBytes(LINE_END); 312 282 313 OutputDisplay.setText( "Transferring data to server");314 283 OutputDisplay.setText(tr("Transferring data to server")); 284 new GpxWriter(out).write(gpxData); 315 285 out.flush(); 316 286 out.writeBytes(LINE_END); 317 318 287 } 319 320 // Taken from Christof Dallermassal java class : 321 public String encodeBase64(String s) { 322 StringBuilder out = new StringBuilder(); 323 for (int i = 0; i < (s.length()+2)/3; ++i) { 324 int l = Math.min(3, s.length()-i*3); 325 String buf = s.substring(i*3, i*3+l); 326 out.append(BASE64_ENC.charAt(buf.charAt(0)>>2)); 327 out.append(BASE64_ENC.charAt((buf.charAt(0) & 0x03) << 4 | (l==1?0:(buf.charAt(1) & 0xf0) >> 4))); 328 out.append(l>1 ? BASE64_ENC.charAt((buf.charAt(1) & 0x0f) << 2 | (l==2 ? 0 : (buf.charAt(2) & 0xc0) >> 6)) : '='); 329 out.append(l>2 ? BASE64_ENC.charAt(buf.charAt(2) & 0x3f) : '='); 330 331 } 332 return out.toString(); 333 } 334 335 // Variables declaration - do not modify//GEN-BEGIN:variables 336 private javax.swing.JButton CancelButton; 337 private javax.swing.JButton OkButton; 338 private javax.swing.JTextArea OutputDisplay; 339 private javax.swing.JCheckBox PublicTrace; 340 private javax.swing.JTextField descriptionfield; 341 private javax.swing.JLabel jLabel1; 342 private javax.swing.JLabel jLabel2; 343 private javax.swing.JLabel jLabel3; 344 private javax.swing.JScrollPane jScrollPane1; 345 private javax.swing.JTextField tagfield; 346 // End of variables declaration//GEN-END:variables 347 348 } 349 No newline at end of file 288 }