Example usage for org.springframework.web.servlet ModelAndView getModel

List of usage examples for org.springframework.web.servlet ModelAndView getModel

Introduction

In this page you can find the example usage for org.springframework.web.servlet ModelAndView getModel.

Prototype

public Map<String, Object> getModel() 

Source Link

Document

Return the model map.

Usage

From source file:ServerConfigurations.server.ProjectConfiguration.Controller.java

@Override
public ModelAndView doHandle(HttpServletRequest request, HttpServletResponse response) {
    try {/*from w  ww.j av  a  2 s.  c  o  m*/
        SUser user = SessionUser.getUser(request);
        SProject project = getProject(request);
        if (project != null && this.isAvailable(user, project)) {

            ProjectConfigurations projectSettings = (ProjectConfigurations) this.projectSettingsManager
                    .getSettings(project.getProjectId(), Util.PLUGIN_NAME);

            String action = request.getParameter("action");
            if (action == null || action.trim().length() == 0)
                action = "list";
            else
                action = action.trim();
            boolean isPost = this.isPost(request);

            if (action.equals("list")) {

                ModelAndView modelAndView = new ModelAndView(this.listPagePath);
                modelAndView.getModel().put("pluginName", Util.PLUGIN_NAME);
                modelAndView.getModel().put("controllerPath", CONTROLLER_PATH);
                modelAndView.getModel().put("projectId", project.getExternalId());
                modelAndView.getModel().put("configurations", projectSettings.getConfigurationsList());
                return modelAndView;

            } else if (action.equals("edit")) {

                String configurationPrefix = request.getParameter("prefix");
                String configurationBranchFilter = this.hasRequestParameter(request, "branchFilter")
                        ? request.getParameter("branchFilter")
                        : "";
                if (configurationPrefix != null) {
                    ProjectConfiguration configuration = projectSettings.getConfigurationByPrefixBranchFilter(
                            configurationPrefix, configurationBranchFilter);
                    if (configuration != null) {
                        ModelAndView modelAndView = new ModelAndView(this.editPagePath);
                        modelAndView.getModel().put("pluginName", Util.PLUGIN_NAME);
                        modelAndView.getModel().put("controllerPath", CONTROLLER_PATH);
                        modelAndView.getModel().put("projectId", project.getExternalId());
                        for (Map.Entry<String, String> entry : configuration.getConfigurationMap().entrySet()) {
                            modelAndView.getModel().put(entry.getKey(), entry.getValue());
                        }
                        modelAndView.getModel().put("configurations",
                                this.configurations.getConfigurationsList());
                        return modelAndView;
                    }
                }

            } else if (action.equals("new")) {

                ModelAndView modelAndView = new ModelAndView(this.editPagePath);
                modelAndView.getModel().put("pluginName", Util.PLUGIN_NAME);
                modelAndView.getModel().put("controllerPath", CONTROLLER_PATH);
                modelAndView.getModel().put("projectId", project.getExternalId());
                modelAndView.getModel().put("configurations", this.configurations.getConfigurationsList());
                return modelAndView;

            } else if (action.equals("save") && isPost) {

                // Check required fields
                if (!this.hasRequestParameter(request, "prefix")
                        && !this.hasRequestParameter(request, "initialPrefix")) {
                    return this.sendError(request, response, "Prefix field cannot be empty");
                }
                if (!this.hasRequestParameter(request, "name")) {
                    return this.sendError(request, response, "Choose server configuration");
                }
                String branchFilter = this.hasRequestParameter(request, "branchFilter")
                        ? request.getParameter("branchFilter")
                        : "";
                ProjectConfiguration configuration;
                if (!this.hasRequestParameter(request, "initialPrefix")) {
                    if (projectSettings.getConfigurationByPrefixBranchFilter(request.getParameter("prefix"),
                            branchFilter) != null) {
                        return this.sendError(request, response,
                                "There is another configuration with such prefix and branch filter, please choose another");
                    } else {
                        configuration = projectSettings.newConfiguration();
                    }
                } else {
                    configuration = projectSettings.getConfigurationByPrefixBranchFilter(
                            request.getParameter("initialPrefix"), request.getParameter("initialBranchFilter"));
                }
                configuration.setName(request.getParameter("name"));
                configuration.setPrefix(request.getParameter("prefix"));
                configuration.setBranchFilter(branchFilter);
                project.persist();
                return this.sendMessage(request, response, "Project server configuration saved successfully");

            } else if (action.equals("delete")) {

                ProjectConfiguration configuration = projectSettings.getConfigurationByPrefixBranchFilter(
                        request.getParameter("prefix"), request.getParameter("branchFilter"));
                projectSettings.deleteConfiguration(configuration);
                project.persist();
                return new ModelAndView(new RedirectView("/admin/editProject.html?projectId="
                        + project.getExternalId() + "&tab=" + Util.PLUGIN_NAME, true));

            }
        } else {
            WebAuthUtil.addAccessDeniedMessage(request, new AccessDeniedException(user,
                    "You must be an System Administrator or ProjectConfiguration Administrator to manage server configuration."));
            return null;
        }
    } catch (Exception e) {
        Loggers.SERVER.error(Util.PLUGIN_NAME + ": Could not handle request", e);
    }

    return null;
}

From source file:com.carlos.projects.billing.ui.controllers.LoadComponentsControllerTest.java

@SuppressWarnings({ "unchecked" })
@Test//  ww w.java2 s.  c  om
public void shouldLoadComponentsAndFamilyNameForAGivenFamilyCode() throws Exception {
    // Given
    Set<Component> components = mockComponents();
    String familyName = "Family name";
    when(family.getComponents()).thenReturn(components);
    when(family.getDescription()).thenReturn(familyName);

    // When
    ModelAndView modelAndView = controller.handleRequest(request, response);

    // Then
    verify(familyDao).getById(Family.class, familyCode);
    verify(family).getComponents();
    verify(request).getParameter("familyCode");
    assertThat((Set<Component>) modelAndView.getModel().get("components"), is(components));
    assertThat((String) modelAndView.getModel().get("familyName"), is(familyName));
    assertThat(modelAndView.getViewName(), is(controller.getViewName()));
}

From source file:no.dusken.common.plugin.control.admin.PluginStoreControllerTest.java

@Test
public void testSubmit() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/pluginstore");
    MockHttpServletResponse response = new MockHttpServletResponse();

    request.setParameter("no.dusken.common.plugin.p1-setting1", "newValue1p1");
    request.setParameter("no.dusken.common.plugin.p1-setting2", "newValue2p1");
    request.setParameter("no.dusken.common.plugin.p2-setting1", "newValue1p2");
    request.setParameter("no.dusken.common.plugin.p2-setting2", "newValue2p2");
    ModelAndView mav = pluginStoreController.handleRequestInternal(request, response);

    Map<DuskenPlugin, Map<String, String>> plugins = (Map<DuskenPlugin, Map<String, String>>) mav.getModel()
            .get("plugins");
    assertNotNull("Pluginsmap was null", plugins);
    assertEquals("Pluginsmap contained wrong number of plugins", 2, plugins.size());
    for (DuskenPlugin p : plugins.keySet()) {
        PluginStore store = pluginStoreProvider.getStore(p);
        String setting1 = store.getString("setting1", "default");
        String setting2 = store.getString("setting2", "default");

        assertEquals("Setting1 has wrong value", "newValue1".concat(p.getPluginName(null)), setting1);
        assertEquals("Setting2 has wrong value", "newValue2".concat(p.getPluginName(null)), setting2);
    }//from w  w w.j  av a  2  s .co m

    Map updatemap = (Map) mav.getModel().get("updatemap");
    assertNotNull("Map did not contain updatemap", updatemap);
    assertEquals("Wrong number of updatestrings", 4, updatemap.size());
}

From source file:ch.silviowangler.dox.web.DocumentControllerTest.java

@Test
@SuppressWarnings("unchecked")
public void edit() throws Exception {

    final DocumentClass documentClass = new DocumentClass("hello");
    final DocumentReference documentReference = newDocumentReference("hello.txt")
            .withDocumentClass(documentClass).build();

    when(documentService.findDocumentReference(1L)).thenReturn(documentReference);
    SortedSet<Attribute> attributes = new TreeSet<>();
    attributes.add(new Attribute("a", false, AttributeDataType.CURRENCY));
    attributes.add(new Attribute("b", false, AttributeDataType.STRING));
    when(documentService.findAttributes(documentClass)).thenReturn(attributes);

    final ModelAndView modelAndView = controller.editDocument(1L);

    assertThat(modelAndView.getViewName(), is("edit.doc"));
    assertThat(modelAndView.getModel().size(), is(2));
    assertThat(modelAndView.getModel().containsKey("doc"), is(true));
    assertThat((DocumentReference) modelAndView.getModel().get("doc"), is(documentReference));
    assertThat(modelAndView.getModel().containsKey("attributes"), is(true));
    assertThat(((SortedSet<Attribute>) modelAndView.getModel().get("attributes")).size(), is(2));

    InOrder order = inOrder(documentService);
    order.verify(documentService).findDocumentReference(1L);
    order.verify(documentService).findAttributes(documentReference.getDocumentClass());
    order.verifyNoMoreInteractions();/*from ww  w  .  jav  a2  s.com*/
}

From source file:no.dusken.aranea.admin.control.EditPageController.java

@Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object object,
        BindException errors) throws Exception {

    Page page = (Page) object;/*from   w w w  .ja  va 2 s. co m*/

    for (PageImage pi : page.getPageImages()) {
        if (request.getParameter("deleteEntry" + pi.getID()) != null) {
            // we are to delete this entry
            page.removeEntry(pi);
            if (pi == page.getFrontpageImage()) {
                page.setFrontpageImage(null);
                //TODO remove this hack, it should be handled be the db and cascading
                pageService.saveOrUpdate(page);
            }
            // the entity is detached (?) entach it
            pi = pageImageService.getEntity(pi.getID());
            // delete the entry from the database
            pageImageService.remove(pi);
            // go back to the edit view
            setSuccessView(getFormView());
            // do not continue the loop
            break;
        } else if (request.getParameter("setAsMainImage" + pi.getID()) != null) {
            page.setFrontpageImage(pi);
            // do not continue the loop
            break;
        }
    }
    if (request.getParameter("unSetMainImage") != null) {
        page.setFrontpageImage(null);
    }
    // Detatch frontpage image if we are going to delete.
    if (request.getParameter("delete") != null) {
        page.setFrontpageImage(null);
        genericService.saveOrUpdate(page);
    }
    page.setModified(Calendar.getInstance());
    boolean createSectionPage = false;
    if (page.isNew()) {
        createSectionPage = true;
    }
    ModelAndView mav = super.onSubmit(request, response, page, errors);
    page = (Page) mav.getModel().get("entity");
    if (createSectionPage) {
        // At this point we expect that the object has been saved to the database and that the
        // reference is valid
        createSectionPage(page);
    }
    return mav;
}

From source file:org.guanxi.sp.engine.service.shibboleth.AuthConsumerService.java

/**
 * This checks the status of the thread associated with this request. This will display
 * either a please wait message (with progress bar) or will forward the user to the
 * Podder./* ww w. j  ava  2 s.  co  m*/
 *
 * @param request  the HttpServletRequest
 * @param response the HttpServletResponse
 * @return the ModelAndView
 */
@SuppressWarnings("unchecked")
public ModelAndView process(HttpServletRequest request, HttpServletResponse response) {
    AuthConsumerServiceThread thread;
    HttpSession session;

    session = request.getSession(false);
    if (session == null) {
        ModelAndView mAndV;

        mAndV = new ModelAndView();
        mAndV.setViewName(errorView);
        mAndV.getModel().put(errorViewDisplayVar, "Your session has expired");

        return mAndV;
    }

    thread = threads.get(session);
    if (thread == null) {
        ModelAndView mAndV;

        mAndV = new ModelAndView();
        mAndV.setViewName(errorView);
        mAndV.getModel().put(errorViewDisplayVar, "Processing thread cannot be found");

        return mAndV;
    }
    if (thread.isCompleted()) {
        threads.remove(session); // TODO: Periodic unloading of expired threads?
    }
    return thread.getStatus();
}

From source file:org.jasig.cas.web.ServiceValidateControllerTests.java

@Test
public void testValidServiceTicketWithBadPgt() throws Exception {
    this.serviceValidateController.setProxyHandler(new Cas10ProxyHandler());
    final String tId = getCentralAuthenticationService()
            .createTicketGrantingTicket(TestUtils.getCredentialsWithSameUsernameAndPassword());
    final String sId = getCentralAuthenticationService().grantServiceTicket(tId, TestUtils.getService());

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("service", TestUtils.getService().getId());
    request.addParameter("ticket", sId);
    request.addParameter("pgtUrl", "http://www.acs.rutgers.edu");

    final ModelAndView modelAndView = this.serviceValidateController.handleRequestInternal(request,
            new MockHttpServletResponse());
    assertEquals(CONST_SUCCESS_VIEW, modelAndView.getViewName());
    assertNull(modelAndView.getModel().get("pgtIou"));
}

From source file:org.jasig.cas.web.ServiceValidateControllerTests.java

@Test
public void testValidServiceTicketWithInvalidPgt() throws Exception {
    this.serviceValidateController.setProxyHandler(new Cas10ProxyHandler());
    final String tId = getCentralAuthenticationService()
            .createTicketGrantingTicket(TestUtils.getCredentialsWithSameUsernameAndPassword());
    final String sId = getCentralAuthenticationService().grantServiceTicket(tId, TestUtils.getService());

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("service", TestUtils.getService().getId());
    request.addParameter("ticket", sId);
    request.addParameter("pgtUrl", "duh");

    final ModelAndView modelAndView = this.serviceValidateController.handleRequestInternal(request,
            new MockHttpServletResponse());
    assertEquals(CONST_SUCCESS_VIEW, modelAndView.getViewName());
    assertNull(modelAndView.getModel().get("pgtIou"));
}

From source file:net.nicholaswilliams.java.teamcity.plugin.buildNumber.SharedBuildNumberController.java

protected ModelAndView submitAddBuildNumber(HttpServletRequest request) throws IOException {
    SharedBuildNumber form = new SharedBuildNumber(0);
    BindingResult result = new BeanPropertyBindingResult(form, SharedBuildNumberController.FORM);
    this.bindAndValidateForm(request, result);

    if (result.hasErrors()) {
        ModelAndView modelAndView = new ModelAndView(this.addJspPagePath);

        modelAndView.getModel().put(BindingResult.MODEL_KEY_PREFIX + SharedBuildNumberController.FORM, result);
        modelAndView.getModel().put(SharedBuildNumberController.FORM, form);

        return modelAndView;
    }//  w  ww .  j av a2 s  .  co m

    SharedBuildNumber buildNumber = new SharedBuildNumber(this.configurationService.getNextBuildNumberId());
    this.copyFormToBuildNumber(form, buildNumber);
    this.configurationService.saveSharedBuildNumber(buildNumber);

    return this.returnRedirectView("/admin/admin.html?item=sharedBuildNumbers");
}

From source file:ch.silviowangler.dox.web.DocumentControllerTest.java

@Test
public void editDocumentWithParams() throws DocumentNotFoundException {

    final DocumentReference documentReference = newDocumentReference("hello.txt").withDocumentClass("test")
            .withIndex("name", "Silvio").build();

    when(documentService.findDocumentReference(1L)).thenReturn(documentReference);
    when(documentService.updateIndices(documentReference)).thenReturn(documentReference);

    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setParameter("name", "Wangler");
    final ModelAndView modelAndView = controller.editDocument(1L, request);

    assertThat(modelAndView.getViewName(), is("import.successful"));
    assertThat(modelAndView.getModel().size(), is(1));
    assertThat(modelAndView.getModel().containsKey("doc"), is(true));
    final DocumentReference doc = (DocumentReference) modelAndView.getModel().get("doc");
    assertThat(doc, is(documentReference));
    assertThat(doc.getIndices().get(new TranslatableKey("name")).getValue().toString(), is("Wangler"));

    InOrder order = inOrder(documentService);

    order.verify(documentService).findDocumentReference(1L);
    order.verify(documentService).updateIndices(documentReference);
    order.verifyNoMoreInteractions();/*from   ww  w.ja  v  a2  s . c o  m*/
}