Changeset 32528 in osm for applications/editors/josm/plugins/imagery_offset_db/src/iodb/SimpleOffsetQueryTask.java
- Timestamp:
- 2016-07-02T03:55:03+02:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/imagery_offset_db/src/iodb/SimpleOffsetQueryTask.java
r29450 r32528 1 // License: WTFPL. For details, see LICENSE file. 1 2 package iodb; 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 2 5 3 6 import java.io.IOException; 4 7 import java.io.InputStream; 5 import java.net.*; 6 import java.util.*; 7 import java.util.regex.*; 8 import java.net.HttpURLConnection; 9 import java.net.MalformedURLException; 10 import java.net.URL; 11 import java.util.Scanner; 12 import java.util.regex.Matcher; 13 import java.util.regex.Pattern; 14 8 15 import javax.swing.JOptionPane; 16 9 17 import org.openstreetmap.josm.Main; 10 18 import org.openstreetmap.josm.gui.PleaseWaitRunnable; 11 import static org.openstreetmap.josm.tools.I18n.tr;12 19 13 20 /** … … 29 36 * @param title A title for the progress monitor. 30 37 */ 31 publicSimpleOffsetQueryTask(38 SimpleOffsetQueryTask(String query, String title) { 32 39 super(ImageryOffsetTools.DIALOG_TITLE); 33 40 this.query = query; … … 39 46 * In case a query was not specified when the object was constructed, 40 47 * it can be set with this method. 41 * @see #SimpleOffsetQueryTask(java.lang.String, java.lang.String) 48 * @see #SimpleOffsetQueryTask(java.lang.String, java.lang.String) 42 49 */ 43 public void setQuery( 50 public void setQuery(String query) { 44 51 this.query = query; 45 52 } … … 48 55 * Install a listener for successful responses. There can be only one. 49 56 */ 50 public void setListener( 57 public void setListener(QuerySuccessListener listener) { 51 58 this.listener = listener; 52 59 } … … 68 75 errorMessage = null; 69 76 doQuery(query); 70 } catch (UploadException e77 } catch (UploadException e) { 71 78 errorMessage = tr("Server has rejected the request") + ":\n" + e.getMessage(); 72 } catch (IOException e79 } catch (IOException e) { 73 80 errorMessage = tr("Unable to connect to the server") + "\n" + e.getMessage(); 74 81 } … … 78 85 * Sends a request to the imagery offset server. Processes exceptions and 79 86 * return codes, calls {@link #processResponse(java.io.InputStream)} on success. 80 * @param query81 * @throws iodb.SimpleOffsetQueryTask.UploadException82 * @throws IOException83 87 */ 84 private void doQuery( 88 private void doQuery(String query) throws UploadException, IOException { 85 89 try { 86 90 String serverURL = Main.pref.get("iodb.server.url", "http://offsets.textual.ru/"); 87 91 URL url = new URL(serverURL + query); 88 // Main.info("IODB URL = " + url); // todo: remove in release 89 HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 92 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 90 93 connection.connect(); 91 if (connection.getResponseCode() != 20094 if (connection.getResponseCode() != 200) { 92 95 throw new IOException("HTTP Response code " + connection.getResponseCode() + " (" + connection.getResponseMessage() + ")"); 93 96 } 94 97 InputStream inp = connection.getInputStream(); 95 if (inp == null98 if (inp == null) 96 99 throw new IOException("Empty response"); 97 100 try { 98 if (!cancelled101 if (!cancelled) 99 102 processResponse(inp); 100 103 } finally { 101 104 connection.disconnect(); 102 105 } 103 } catch (MalformedURLException ex106 } catch (MalformedURLException ex) { 104 107 throw new IOException("Malformed URL: " + ex.getMessage()); 105 108 } … … 120 123 @Override 121 124 protected void finish() { 122 if (errorMessage != null125 if (errorMessage != null) { 123 126 JOptionPane.showMessageDialog(Main.parent, errorMessage, 124 127 ImageryOffsetTools.DIALOG_TITLE, JOptionPane.ERROR_MESSAGE); 125 } else if (listener != null128 } else if (listener != null) { 126 129 listener.queryPassed(); 127 130 } … … 133 136 * @throws iodb.SimpleOffsetQueryTask.UploadException Thrown if an error message was found. 134 137 */ 135 protected void processResponse( 138 protected void processResponse(InputStream inp) throws UploadException { 136 139 String response = ""; 137 if (inp != null140 if (inp != null) { 138 141 Scanner sc = new Scanner(inp).useDelimiter("\\A"); 139 142 response = sc.hasNext() ? sc.next() : ""; … … 141 144 Pattern p = Pattern.compile("<(\\w+)>([^<]+)</\\1>"); 142 145 Matcher m = p.matcher(response); 143 if (m.find()144 if (m.group(1).equals("error")146 if (m.find()) { 147 if (m.group(1).equals("error")) { 145 148 throw new UploadException(m.group(2)); 146 149 } … … 154 157 */ 155 158 public static class UploadException extends Exception { 156 publicUploadException(159 UploadException(String message) { 157 160 super(message); 158 161 }
Note:
See TracChangeset
for help on using the changeset viewer.