diff --git a/src/org/openstreetmap/josm/plugins/indoorequal/Action.java b/src/org/openstreetmap/josm/plugins/indoorequal/Action.java index 706e713..5446b4e 100644 --- a/src/org/openstreetmap/josm/plugins/indoorequal/Action.java +++ b/src/org/openstreetmap/josm/plugins/indoorequal/Action.java @@ -1,5 +1,6 @@ // License: GPL. For details, see LICENSE file. package org.openstreetmap.josm.plugins.indoorequal; + import static org.openstreetmap.josm.tools.I18n.tr; import java.awt.event.ActionEvent; @@ -19,39 +20,39 @@ public class Action extends JosmAction { - public Action() { - super(tr("View in Indoor=..."), null, - tr("View current layer in Indoor="), null, false); - } - - @Override - public void actionPerformed(ActionEvent event) { - Layer layer = MainApplication.getLayerManager().getActiveLayer(); - if (layer == null) - JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(MainApplication.getMainFrame()), - "No default layer found."); - else if (!(layer instanceof OsmDataLayer)) - JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(MainApplication.getMainFrame()), - "The default layer is not an OSM layer."); - else if (MainApplication.getMap() == null) - JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(MainApplication.getMainFrame()), - "No map found."); - else if (MainApplication.getMap().mapView == null) - JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(MainApplication.getMainFrame()), - "No map view found."); - else { - try { - OsmExporter osmExporter = new OsmExporter(); - File osmFile = new File(System.getProperty("java.io.tmpdir") + "/indoorequal.osm"); - osmExporter.exportData(osmFile, (OsmDataLayer) layer); - - String result = OpenBrowser.displayUrl("https://indoorequal.org/#url=http://localhost:8432/indoorequal.osm"); - if (result != null) { - Logging.warn(result); - } - } catch (IOException e) { - Logging.error(e); - } - } - } + public Action() { + super(tr("View in Indoor=..."), null, tr("View current layer in Indoor="), null, false); + } + + @Override + public void actionPerformed(ActionEvent event) { + Layer layer = MainApplication.getLayerManager().getActiveLayer(); + if (layer == null) + JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(MainApplication.getMainFrame()), + "No default layer found."); + else if (!(layer instanceof OsmDataLayer)) + JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(MainApplication.getMainFrame()), + "The default layer is not an OSM layer."); + else if (MainApplication.getMap() == null) + JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(MainApplication.getMainFrame()), + "No map found."); + else if (MainApplication.getMap().mapView == null) + JOptionPane.showMessageDialog(JOptionPane.getFrameForComponent(MainApplication.getMainFrame()), + "No map view found."); + else { + try { + OsmExporter osmExporter = new OsmExporter(); + File osmFile = new File(System.getProperty("java.io.tmpdir") + "/indoorequal.osm"); + osmExporter.exportData(osmFile, (OsmDataLayer) layer); + + String result = OpenBrowser + .displayUrl("https://indoorequal.org/#url=http://localhost:8432/indoorequal.osm"); + if (result != null) { + Logging.warn(result); + } + } catch (IOException e) { + Logging.error(e); + } + } + } } diff --git a/src/org/openstreetmap/josm/plugins/indoorequal/HttpHandler.java b/src/org/openstreetmap/josm/plugins/indoorequal/HttpHandler.java index bd5593c..6b3993a 100644 --- a/src/org/openstreetmap/josm/plugins/indoorequal/HttpHandler.java +++ b/src/org/openstreetmap/josm/plugins/indoorequal/HttpHandler.java @@ -17,85 +17,84 @@ import org.openstreetmap.josm.tools.Logging; public class HttpHandler implements Runnable { - private Socket socket; + private Socket socket; - public HttpHandler(Socket socket) { - this.socket = socket; - } + public HttpHandler(Socket socket) { + this.socket = socket; + } - @Override - public void run() { - try { - handleRequest(); - } catch (Exception e) { - Logging.error("Error Occured: " + e.getMessage()); - try { - socket.close(); - } catch (IOException e1) { - Logging.error("Error Closing socket Connection."); - } - Logging.error("OpenIndoor is Terminating!"); - } - } + @Override + public void run() { + try { + handleRequest(); + } catch (Exception e) { + Logging.error("Error Occured: " + e.getMessage()); + try { + socket.close(); + } catch (IOException e1) { + Logging.error("Error Closing socket Connection."); + } + Logging.error("OpenIndoor is Terminating!"); + } + } - private void handleRequest() throws Exception { - InputStream input; - OutputStream output; + private void handleRequest() throws Exception { + InputStream input; + OutputStream output; - File root = new File(System.getProperty("java.io.tmpdir")); - if (root.isDirectory()) { - input = socket.getInputStream(); - output = socket.getOutputStream(); - serverRequest(input, output, root.toString()); - output.close(); - input.close(); - } else { - throw new Exception("www directory not present!"); - } - socket.close(); - } + File root = new File(System.getProperty("java.io.tmpdir")); + if (root.isDirectory()) { + input = socket.getInputStream(); + output = socket.getOutputStream(); + serverRequest(input, output, root.toString()); + output.close(); + input.close(); + } else { + throw new Exception("www directory not present!"); + } + socket.close(); + } - private void serverRequest(InputStream input, OutputStream output, String root) throws Exception { - String line; - BufferedReader bf = new BufferedReader(new InputStreamReader(input)); - while ((line = bf.readLine()) != null) { - if (line.length() <= 0) { - break; - } - if (line.startsWith("GET")) { - String filename= line.split(" ")[1].substring(1); - File resource = new File(root + File.separator + filename); - if (resource.isFile()) { - populateResponse(resource, output); - } else { - String Content_NOT_FOUND = "

File Not Found

"; + private void serverRequest(InputStream input, OutputStream output, String root) throws Exception { + String line; + BufferedReader bf = new BufferedReader(new InputStreamReader(input)); + while ((line = bf.readLine()) != null) { + if (line.length() <= 0) { + break; + } + if (line.startsWith("GET")) { + String filename = line.split(" ")[1].substring(1); + File resource = new File(root + File.separator + filename); + if (resource.isFile()) { + populateResponse(resource, output); + } else { + String Content_NOT_FOUND = "

File Not Found

"; - String REQ_NOT_FOUND = "HTTP/1.1 404 Not Found\n\n"; - String header = REQ_NOT_FOUND+ Content_NOT_FOUND; + String REQ_NOT_FOUND = "HTTP/1.1 404 Not Found\n\n"; + String header = REQ_NOT_FOUND + Content_NOT_FOUND; - output.write(header.getBytes()); - } - break; - } - } - } + output.write(header.getBytes()); + } + break; + } + } + } - private void populateResponse(File resource, OutputStream output) throws IOException { - SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z"); - format.setTimeZone(TimeZone.getTimeZone("GMT")); + private void populateResponse(File resource, OutputStream output) throws IOException { + SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z"); + format.setTimeZone(TimeZone.getTimeZone("GMT")); - String REQ_FOUND = "HTTP/1.0 200 OK\n"; - String SERVER = "Server: HTTP server/0.1\n"; - String DATE = "Date: " + format.format(new java.util.Date()) + "\n"; - String CORS = "Access-Control-Allow-Origin: *\n"; - String CONTENT_TYPE = "Content-type: " + URLConnection.guessContentTypeFromName(resource.getName()); - String LENGTH = "Content-Length: " + resource.length() + "\n\n"; + String REQ_FOUND = "HTTP/1.0 200 OK\n"; + String SERVER = "Server: HTTP server/0.1\n"; + String DATE = "Date: " + format.format(new java.util.Date()) + "\n"; + String CORS = "Access-Control-Allow-Origin: *\n"; + String CONTENT_TYPE = "Content-type: " + URLConnection.guessContentTypeFromName(resource.getName()); + String LENGTH = "Content-Length: " + resource.length() + "\n\n"; + String header = REQ_FOUND + SERVER + DATE + CORS + CONTENT_TYPE + LENGTH; + output.write(header.getBytes()); - String header = REQ_FOUND + SERVER + DATE + CORS + CONTENT_TYPE + LENGTH; - output.write(header.getBytes()); - - Files.copy(Paths.get(resource.toString()), output); - output.flush(); - } + Files.copy(Paths.get(resource.toString()), output); + output.flush(); + } } diff --git a/src/org/openstreetmap/josm/plugins/indoorequal/IndoorEqualPlugin.java b/src/org/openstreetmap/josm/plugins/indoorequal/IndoorEqualPlugin.java index 3719dbd..e7b7207 100644 --- a/src/org/openstreetmap/josm/plugins/indoorequal/IndoorEqualPlugin.java +++ b/src/org/openstreetmap/josm/plugins/indoorequal/IndoorEqualPlugin.java @@ -2,50 +2,44 @@ package org.openstreetmap.josm.plugins.indoorequal; import java.io.IOException; + import javax.swing.JMenu; import javax.swing.JMenuItem; import org.openstreetmap.josm.gui.MainApplication; import org.openstreetmap.josm.plugins.Plugin; import org.openstreetmap.josm.plugins.PluginInformation; - import org.openstreetmap.josm.tools.Logging; /** * Indoor= plugin. */ public final class IndoorEqualPlugin extends Plugin { + private static int PORT = 8432; + + public IndoorEqualPlugin(PluginInformation info) { + super(info); + + startServer(); + refreshMenu(); + } + + public static void startServer() { + try { + Server server = new Server(PORT); + server.start(); + } catch (IOException e) { + Logging.error("Error occured:" + e.getMessage()); + } + } - private static IndoorEqualPlugin instance; - private static int PORT = 8432; - - public IndoorEqualPlugin(PluginInformation info) { - super(info); - if (instance == null) { - instance = this; - try { - Server server = new Server(PORT); - server.start(); - } catch (IOException e) { - Logging.error("Error occured:" + e.getMessage()); - } - refreshMenu(); - } else { - throw new IllegalStateException("Cannot instantiate plugin twice !"); - } - } - - public static IndoorEqualPlugin getInstance() { - return instance; - } - - public static void refreshMenu() { - JMenu menu = MainApplication.getMenu().moreToolsMenu; - if (menu.isVisible()) - menu.addSeparator(); - else { - menu.setVisible(true); - } - menu.add(new JMenuItem(new Action())); - } + public static void refreshMenu() { + JMenu menu = MainApplication.getMenu().moreToolsMenu; + if (menu.isVisible()) + menu.addSeparator(); + else { + menu.setVisible(true); + } + menu.add(new JMenuItem(new Action())); + } } diff --git a/src/org/openstreetmap/josm/plugins/indoorequal/Server.java b/src/org/openstreetmap/josm/plugins/indoorequal/Server.java index 13d9088..a6758b9 100644 --- a/src/org/openstreetmap/josm/plugins/indoorequal/Server.java +++ b/src/org/openstreetmap/josm/plugins/indoorequal/Server.java @@ -7,28 +7,28 @@ import org.openstreetmap.josm.tools.Logging; public class Server extends Thread { - private ServerSocket serverSocket; - - public Server(int port) throws IOException { - serverSocket = new ServerSocket(port); - } + private ServerSocket serverSocket; - private void start_() throws IOException { - while (true) { - Socket socket = serverSocket.accept(); - HttpHandler connection = new HttpHandler(socket); + public Server(int port) throws IOException { + serverSocket = new ServerSocket(port); + } - Thread request = new Thread(connection); - request.start(); - } - } + private void start_() throws IOException { + while (true) { + Socket socket = serverSocket.accept(); + HttpHandler connection = new HttpHandler(socket); - @Override - public void run() { - try { - this.start_(); - } catch (IOException e) { - Logging.error(e.getMessage()); - } - } + Thread request = new Thread(connection); + request.start(); + } + } + + @Override + public void run() { + try { + this.start_(); + } catch (IOException e) { + Logging.error(e.getMessage()); + } + } }