source: osm/applications/editors/josm/plugins/DirectUpload/src/org/openstreetmap/josm/plugins/DirectUpload/UploadDataGui.java@ 13373

Last change on this file since 13373 was 13373, checked in by stoecker, 15 years ago

apply patch by xeen for bug #1635

File size: 10.8 KB
Line 
1/*
2 * UploadDataGui.java
3 *
4 * Created on August 17, 2008, 6:56 PM
5 * Copyright by Subhodip Biswas
6 * This program is free software and licensed under GPL.
7 */
8
9package org.openstreetmap.josm.plugins.DirectUpload;
10
11import java.awt.BorderLayout;
12import java.awt.Container;
13import java.awt.Dimension;
14import java.awt.event.ItemEvent;
15import java.awt.GridBagLayout;
16import java.awt.GridBagConstraints;
17import java.awt.Toolkit;
18import java.io.BufferedOutputStream;
19import java.io.DataOutputStream;
20import java.io.IOException;
21import java.io.UnsupportedEncodingException;
22import java.net.HttpURLConnection;
23import java.net.MalformedURLException;
24import java.net.URL;
25import java.text.DateFormat;
26import java.text.SimpleDateFormat;
27import java.util.Date;
28import java.util.logging.Level;
29import java.util.logging.Logger;
30
31import javax.swing.BorderFactory;
32import javax.swing.Box;
33import javax.swing.JButton;
34import javax.swing.JCheckBox;
35import javax.swing.JLabel;
36import javax.swing.JOptionPane;
37import javax.swing.JPanel;
38import javax.swing.JScrollPane;
39import javax.swing.JTextArea;
40import javax.swing.JTextField;
41import javax.swing.UIManager;
42
43import org.openstreetmap.josm.Main;
44import org.openstreetmap.josm.data.gpx.GpxData;
45import org.openstreetmap.josm.io.GpxWriter;
46import org.openstreetmap.josm.gui.layer.Layer;
47import org.openstreetmap.josm.gui.layer.GpxLayer;
48import org.openstreetmap.josm.gui.MapView;
49import org.openstreetmap.josm.tools.Base64;
50import org.openstreetmap.josm.tools.GBC;
51import static org.openstreetmap.josm.tools.I18n.tr;
52
53/**
54 *
55 * @author subhodip
56 */
57public 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());
68
69 /** Creates new form UploadDataGui */
70 public UploadDataGui() {
71 setTitle(tr("Upload Traces"));
72 initComponents();
73 }
74
75 private void initComponents() {
76 setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
77 setAlwaysOnTop(true);
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();
93 jScrollPane1.setViewportView(OutputDisplay);
94
95 JButton OkButton = new JButton(tr("Upload GPX track"));
96 OkButton.addActionListener(new java.awt.event.ActionListener() {
97 public void actionPerformed(java.awt.event.ActionEvent evt) {
98 OkButtonActionPerformed(evt);
99 }
100 });
101
102 JButton CancelButton = new JButton(tr("Cancel"));
103 CancelButton.addActionListener(new java.awt.event.ActionListener() {
104 public void actionPerformed(java.awt.event.ActionEvent evt) {
105 CancelButtonActionPerformed(evt);
106 }
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.");
116
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());
122
123 p.add(OutputDisplay, GBC.eol().fill());
124
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));
130
131 p.add(publicCheckbox, GBC.eol());
132
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
139 pack();
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 }
160 }
161
162 boolean x=checkForGPXLayer();
163 }
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"));
173
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();
196
197 int returnCode = connect.getResponseCode();
198 String returnMsg = connect.getResponseMessage();
199 //System.err.println(returnCode);
200 OutputDisplay.setText(returnMsg);
201
202 if (returnCode != 200) {
203 if (connect.getHeaderField("Error") != null)
204 returnMsg += "\n" + connect.getHeaderField("Error");
205 connect.disconnect();
206 }
207 out.close();
208 connect.disconnect();
209
210 } catch(UnsupportedEncodingException ignore) {
211 } catch (MalformedURLException e) {
212 OutputDisplay.setText(tr("Error while uploading"));
213 e.printStackTrace();
214 }
215 }
216
217
218 private boolean checkForErrors(String username, String password, String description, GpxData gpxData) {
219 String errors="";
220 if(description == null || description.length() == 0)
221 errors += tr("No description provided. Please provide some description.");
222
223 if(gpxData == null)
224 errors += tr("No GPX layer selected. Cannot upload a trace.");
225
226 if(username == null || username.length()==0)
227 errors += tr("No username provided.");
228
229 if(password == null || password.length()==0)
230 errors += tr("No password provided.");
231
232 OutputDisplay.setText(errors);
233 return errors.length() > 0;
234 }
235
236 private boolean checkForGPXLayer() {
237 if(Main.map == null || Main.map.mapView == null || Main.map.mapView.getActiveLayer() == null || !(Main.map.mapView.getActiveLayer() instanceof GpxLayer)) {
238 OutputDisplay.setText(tr("No GPX layer selected. Cannot upload a trace."));
239 return true;
240 }
241 return false;
242 }
243
244 private void OkButtonActionPerformed(java.awt.event.ActionEvent evt) {
245 if(checkForGPXLayer()) return;
246
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 }
259 }
260
261 private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {
262 dispose();
263 }
264
265 private void writeContentDisposition(DataOutputStream out, String name, String value) throws IOException {
266 out.writeBytes("--" + BOUNDARY + LINE_END);
267 out.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"" + LINE_END);
268 out.writeBytes(LINE_END);
269 out.writeBytes(value + LINE_END);
270 }
271
272 private void writeContentDispositionGpxData(DataOutputStream out, String name, GpxData gpxData) throws IOException {
273 out.writeBytes("--" + BOUNDARY + LINE_END);
274 out.writeBytes("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + datename +".gpx" + "\"" + LINE_END);
275 out.writeBytes("Content-Type: application/octet-stream" + LINE_END);
276 out.writeBytes(LINE_END);
277
278 OutputDisplay.setText(tr("Transferring data to server"));
279 new GpxWriter(out).write(gpxData);
280 out.flush();
281 out.writeBytes(LINE_END);
282 }
283}
Note: See TracBrowser for help on using the repository browser.