MapController { @SuppressWarnings("OverlyBroadCatchBlock") @RequestMapping(value = PROXY_PATH, method = RequestMethod.GET) @ResponseBody public String getMapData( @RequestParam(URL_PARAM) final String url, @RequestParam(CALLBACK_PARAM) final String callback ) { if (!CALLBACK_FUNCTION_NAME_PATTERN.matcher(callback).matches()) { throw new IllegalArgumentException("Invalid callback function name"); } try { final String tileUrlTemplate = configService.getConfig().getMap().getTileUrlTemplate(); final URL targetURL = new URL(url); final URL validationURL = new URL(tileUrlTemplate); if (!validationURL.getProtocol().equals(targetURL.getProtocol()) || !validationURL.getHost().equals(targetURL.getHost()) || validationURL.getPort() != targetURL.getPort()) { throw new IllegalArgumentException("We only allow proxying to the tile server"); } final URLConnection urlConnection = targetURL.openConnection(); final String contentType = urlConnection.getContentType(); return getMapData(callback, urlConnection, contentType); } catch (final IOException e) { log.error("Error retrieving map data", e); return callback + "(\"error:Application error\")"; } } @Autowired  MapController(@SuppressWarnings("SpringJavaAutowiringInspection") final ConfigService<? extends FindConfig<?, ?>> configService); @SuppressWarnings("OverlyBroadCatchBlock") @RequestMapping(value = PROXY_PATH, method = RequestMethod.GET) @ResponseBody String getMapData(
            @RequestParam(URL_PARAM) final String url,
            @RequestParam(CALLBACK_PARAM) final String callback
    );  }
@Test public void getMapData() { assertTrue(EXPECTED_SUCCESS_PATTERN.matcher(controller.getMapData(URL, "fn")).matches()); }
@Test public void getMapDataInvalidCallback() { try { controller.getMapData(URL, "0 BAD 0"); fail("Exception should have been thrown"); } catch(final IllegalArgumentException e) { assertThat("Exception has the correct message", e.getMessage(), is("Invalid callback function name")); } }
@Test public void getMapDataInvalidUrl() { try { controller.getMapData("http: fail("Exception should have been thrown"); } catch(final IllegalArgumentException e) { assertThat("Exception has the correct message", e.getMessage(), is("We only allow proxying to the tile server")); } }
@Test public void onError() { final String badUrl = "htp: when(config.getMap()).thenReturn(new MapConfiguration(badUrl, false, null, null, null, null)); assertEquals("fn(\"error:Application error\")", controller.getMapData(badUrl, "fn")); }