GetAppSetupHandler extends ActionHandler { public void handleAction(final ActionParameters params) throws ActionException { final long oldId = params.getHttpParam(PARAM_OLD_ID, -1); final User user = params.getUser(); final long defaultViewId = viewService.getDefaultViewId(user); final View view = getView(params, defaultViewId, oldId); if (view == null) { throw new ActionParamsException("Could not load View"); } final long viewId = view.getId(); final String referer = RequestHelper.getDomainFromReferer(params .getHttpHeader(IOHelper.HEADER_REFERER)); boolean ignoreSavedState = !viewService.isSystemDefaultView(viewId) || params.getHttpParam(PARAM_NO_SAVED_STATE, false) || !srsNamesMatch(params, view); if (!ignoreSavedState) { log.debug("Modifying map view if saved state is available"); modifyView(view, getStateFromCookie(params .getCookie(COOKIE_SAVED_STATE))); } final long creator = view.getCreator(); final long userId = params.getUser().getId(); if (view.isPublic()) { log.info("View ID:", viewId, "created by user", creator, "is public, access granted for user with id", userId); } else if (creator == userId) { log.info("Creator", creator, "granted access to view with ID:", viewId); } else { throw new ActionDeniedException("Denied access to view with ID: " + viewId + " for user with id " + userId + " - View created by user " + creator); } if (view.getType().equals(ViewTypes.PUBLISHED)) { final String pubDomain = view.getPubDomain(); if(ViewHelper.isRefererDomain(referer, pubDomain)) { log.info("Granted access to published view in domain:", pubDomain, "for referer", referer); } else { log.error("Referer: ", params.getHttpHeader("Referer"), " -> ", referer); throw new ActionDeniedException( "Denied access to published view in domain: " + pubDomain + " for referer " + referer); } } updateUsageData(view); final JSONObject configuration = getConfiguration(view); final JSONArray startupSequence = getStartupSequence(view); final ModifierParams modifierParams = new ModifierParams(); modifierParams.setBaseAjaxUrl(EnvHelper.getAPIurl(params)); modifierParams.setConfig(configuration); modifierParams.setActionParams(params); modifierParams.setReferer(referer); modifierParams.setView(view); modifierParams.setStartupSequence(startupSequence); modifierParams.setOldPublishedMap(oldId != -1); modifierParams.setModifyURLs(EnvHelper.isSecure(params)); modifierParams.setAjaxRouteParamName(ActionControl.PARAM_ROUTE); if (ViewTypes.DEFAULT.equals(view.getType()) || ViewTypes.USER.equals(view.getType())) { log.debug("Adding bundles for user", params.getUser()); for(Role r : params.getUser().getRoles()) { List<Bundle> bundles = bundlesForRole.get(r.getName()); if(bundles != null) { for(Bundle b : bundles) { addBundle(modifierParams, b.getName(), b); } } } } int locationModified = 0; for (String paramKey : paramHandlers) { final String value = params.getHttpParam(paramKey); log.debug("Handling parameter", paramKey); modifierParams.setParamValue(value); try { if (value != null && ParamControl.handleParam(paramKey, modifierParams)) { locationModified++; log.debug("Parameter", paramKey, "with value", value, "modified map location"); } } catch (ModifierException ex) { log.warn(ex, "Parameter couldn't be handled:", paramKey, "=", value); } modifierParams.setLocationModified(locationModified > 0); } modifierParams.setLocationModified(locationModified > 0); for (int i = 0; i < startupSequence.length(); i++) { final JSONObject bundle = (JSONObject) startupSequence.opt(i); final String bundleid = bundle.optString("bundlename"); if (bundleHandlers.containsKey(bundleid)) { log.debug("Modifying bundle", bundleid); try { bundleHandlers.get(bundleid).modifyBundle(modifierParams); } catch (ModifierException e) { log.error(e, "Unable to modify bundle:", bundle); } } } try { JSONObject appSetup = new JSONObject(); appSetup.put(KEY_ENV, EnvHelper.getEnvironmentJSON(params, view)); appSetup.put(KEY_STARTUP, startupSequence); appSetup.put(KEY_CONFIGURATION, configuration); ResponseHelper.writeResponse(params, appSetup); } catch (JSONException jsonex) { throw new ActionException("Malformed startup sequence/config!", jsonex); } }  void setViewService(final ViewService service); void setBundleService(final BundleService service); void init(); void handleAction(final ActionParameters params); static final String PROPERTY_AJAXURL; static final String PARAM_OLD_ID; static final String PARAM_NO_SAVED_STATE; static final String VIEW_DATA; static final String STATE; static final String COOKIE_SAVED_STATE; }
@Test public void testURLsWithRegistrationAllowed() throws Exception { PropertyUtil.addProperty("allow.registration", "true"); final ActionParameters params = createActionParams(); handler.handleAction(params); JSONObject responseUrls = getResponseJSON().optJSONObject("env").optJSONObject("urls"); assertEquals("Should have default register url", "/user", responseUrls.opt("register")); assertEquals("Should have default profile url", "/user", responseUrls.opt("profile")); teardown(); addLocales(); }
@Test public void testLoginUrl() throws Exception { PropertyUtil.addProperty("auth.login.url", "/login"); final ActionParameters params = createActionParams(); handler.handleAction(params); JSONObject responseUrls = getResponseJSON().optJSONObject("env").optJSONObject("urls"); assertEquals("Should have login url", "/login", responseUrls.opt("login")); assertNull("Should NOT have register url as allow.registration not set", responseUrls.opt("register")); teardown(); addLocales(); }
@Test public void testWithNoViewIdAndGuestUser() throws Exception { final ActionParameters params = createActionParams(); handler.handleAction(params); verify(viewService, times(1)).getViewWithConf(2); verifyResponseContent(ResourceHelper.readJSONResource("GetAppSetupHandlerTest-view-guest.json", this)); }
@Test public void testWithNoViewIdAndLoggedInUser() throws Exception { final ActionParameters params = createActionParams(getLoggedInUser()); handler.handleAction(params); verify(viewService, times(1)).getViewWithConf(1); verifyResponseContent(ResourceHelper.readJSONResource("GetAppSetupHandlerTest-view-loggedin.json", this)); }
@Test public void testWithViewIdGiven() throws Exception { Map<String, String> parameters = new HashMap<String, String>(); parameters.put(ActionConstants.PARAM_VIEW_ID, "3"); final ActionParameters params = createActionParams(parameters); handler.handleAction(params); verify(viewService, times(1)).getViewWithConf(3); verifyResponseContent(ResourceHelper.readJSONResource("GetAppSetupHandlerTest-view-3.json", this)); }
@Test public void testWithCoordinateParameterGiven() throws Exception { Map<String, String> parameters = new HashMap<String, String>(); CoordinateParamHandler h = new CoordinateParamHandler(); parameters.put(h.getName(), "123_456"); final ActionParameters params = createActionParams(parameters); handler.handleAction(params); JSONObject responseState = getResponseJSON().optJSONObject("configuration").optJSONObject("mapfull").optJSONObject("state"); assertEquals("Should have requested east", "123", responseState.opt("east")); assertEquals("Should have requested north", "456", responseState.opt("north")); verifyResponseContent(ResourceHelper.readJSONResource("GetAppSetupHandlerTest-coordinate-params.json", this)); }
@Test public void testWithOldIdGiven() throws Exception { Map<String, String> parameters = new HashMap<String, String>(); parameters.put(ActionConstants.PARAM_VIEW_ID, "456"); parameters.put(GetAppSetupHandler.PARAM_OLD_ID, "123"); final ActionParameters params = createActionParams(parameters); handler.handleAction(params); verify(viewService, never()).getViewWithConf(anyLong()); verify(viewService, times(1)).getViewWithConfByOldId(123); }