Package com.ocpsoft.pretty.faces.config.mapping

Examples of com.ocpsoft.pretty.faces.config.mapping.UrlMapping


      // no mappings added
      assertNotNull(config.getMappings());
      assertEquals(1, config.getMappings().size());

      // validate mapping properties
      UrlMapping mapping = config.getMappings().get(0);
      assertEquals("multipleActions", mapping.getId());
      assertEquals("/some/url", mapping.getPattern());
      assertEquals("/view.jsf", mapping.getViewId());
      assertEquals(true, mapping.isOutbound());
      assertEquals(true, mapping.isOnPostback());
      assertEquals(0, mapping.getQueryParams().size());
      assertEquals(2, mapping.getActions().size());
      assertEquals(0, mapping.getPathValidators().size());

      // validate first action
      UrlAction firstAction = mapping.getActions().get(0);
      assertEquals(PhaseId.RENDER_RESPONSE, firstAction.getPhaseId());
      assertNotNull(firstAction.getAction());
      assertEquals(LazyExpression.class, firstAction.getAction().getClass());
      LazyExpression firstActionExpression = (LazyExpression) firstAction.getAction();
      assertEquals(ClassWithMultipleActions.class, firstActionExpression.getBeanClass());
      assertEquals("actionMethod", firstActionExpression.getComponent());

      // validate second action
      UrlAction secondAction = mapping.getActions().get(1);
      assertEquals(PhaseId.INVOKE_APPLICATION, secondAction.getPhaseId());
      assertNotNull(secondAction.getAction());
      assertEquals(LazyExpression.class, secondAction.getAction().getClass());
      LazyExpression secondActionExpression = (LazyExpression) secondAction.getAction();
      assertEquals(ClassWithMultipleActions.class, secondActionExpression.getBeanClass());
View Full Code Here


      // no mappings added
      assertNotNull(config.getMappings());
      assertEquals(1, config.getMappings().size());

      // validate mapping properties
      UrlMapping mapping = config.getMappings().get(0);
      assertEquals("singleQueryParamater", mapping.getId());
      assertEquals("/some/url", mapping.getPattern());
      assertEquals("/view.jsf", mapping.getViewId());
      assertEquals(true, mapping.isOutbound());
      assertEquals(true, mapping.isOnPostback());
      assertEquals(0, mapping.getActions().size());
      assertEquals(1, mapping.getQueryParams().size());
      assertEquals(0, mapping.getPathValidators().size());

      // validate query parameter
      QueryParameter queryParameter = mapping.getQueryParams().get(0);
      assertEquals("myQueryParam", queryParameter.getName());
      assertEquals(true, queryParameter.isOnPostback());

      // validate PrettyExpression
      assertNotNull(queryParameter.getExpression());
View Full Code Here

      // no mappings added
      assertNotNull(config.getMappings());
      assertEquals(1, config.getMappings().size());

      // validate mapping properties
      UrlMapping mapping = config.getMappings().get(0);
      assertEquals("singleQueryParamater", mapping.getId());
      assertEquals("/some/url", mapping.getPattern());
      assertEquals("/view.jsf", mapping.getViewId());
      assertEquals(true, mapping.isOutbound());
      assertEquals(true, mapping.isOnPostback());
      assertEquals(0, mapping.getActions().size());
      assertEquals(1, mapping.getQueryParams().size());
      assertEquals(0, mapping.getPathValidators().size());

      // validate query parameter
      QueryParameter queryParameter = mapping.getQueryParams().get(0);
      assertEquals("myQueryParam", queryParameter.getName());
      assertEquals(false, queryParameter.isOnPostback());
      assertEquals("#{bean.action}", queryParameter.getOnError());
      assertEquals(2, queryParameter.getValidatorIdList().size());
      assertEquals("myValidator myOtherValidator", queryParameter.getValidatorIds());
View Full Code Here

      // no mappings added
      assertNotNull(config.getMappings());
      assertEquals(2, config.getMappings().size());

      // validate mapping properties for mappingA
      UrlMapping mappingA = config.getMappingById("mappingA");
      assertNotNull(mappingA);
      assertEquals("mappingA", mappingA.getId());
      assertEquals("/some/url/a", mappingA.getPattern());
      assertEquals("/view.jsf", mappingA.getViewId());
      assertEquals(true, mappingA.isOutbound());
      assertEquals(true, mappingA.isOnPostback());
      assertEquals(1, mappingA.getActions().size());
      assertEquals(2, mappingA.getQueryParams().size());
      assertEquals(0, mappingA.getPathValidators().size());
      assertEquals("#{multiMappingBean.actionForBoth}",
               mappingA.getActions().get(0).getAction().getELExpression());

      // we don't know the order in which the query parameters are added
      List<String> queryParamExpressionsA = Arrays.asList(
               mappingA.getQueryParams().get(0).getExpression().getELExpression(),
               mappingA.getQueryParams().get(1).getExpression().getELExpression()
               );
      Collections.sort(queryParamExpressionsA);
      assertEquals("#{multiMappingBean.queryParameterForA}", queryParamExpressionsA.get(0));
      assertEquals("#{multiMappingBean.queryParameterForBoth}", queryParamExpressionsA.get(1));

      // validate mapping properties for mappingB
      UrlMapping mappingB = config.getMappingById("mappingB");
      assertNotNull(mappingB);
      assertEquals("mappingB", mappingB.getId());
      assertEquals("/some/url/b", mappingB.getPattern());
      assertEquals("/view.jsf", mappingB.getViewId());
      assertEquals(true, mappingB.isOutbound());
      assertEquals(true, mappingB.isOnPostback());
      assertEquals(2, mappingB.getActions().size());
      assertEquals(1, mappingB.getQueryParams().size());
      assertEquals(0, mappingB.getPathValidators().size());
      assertEquals("#{multiMappingBean.queryParameterForBoth}",
               mappingB.getQueryParams().get(0).getExpression().getELExpression());

      // we don't know the order in which the actions are added
      List<String> actionExpressionsB = Arrays.asList(
               mappingB.getActions().get(0).getAction().getELExpression(),
               mappingB.getActions().get(1).getAction().getELExpression()
               );
      Collections.sort(actionExpressionsB);
      assertEquals("#{multiMappingBean.actionForBoth}", actionExpressionsB.get(0));
      assertEquals("#{multiMappingBean.actionForB}", actionExpressionsB.get(1));
View Full Code Here

   public void injectParameters(final FacesContext context)
   {
      log.trace("Injecting parameters");
      PrettyContext prettyContext = PrettyContext.getCurrentInstance(context);
      URL url = prettyContext.getRequestURL();
      UrlMapping mapping = prettyContext.getCurrentMapping();

      if (mapping != null)
      {
         injectPathParams(context, url, mapping);
         injectQueryParams(context, mapping, prettyContext);
View Full Code Here

    * @return JSF viewID to which this request resolves.
    */
   public String computeDynaViewId(final FacesContext facesContext)
   {
      PrettyContext context = PrettyContext.getCurrentInstance(facesContext);
      UrlMapping urlMapping = context.getCurrentMapping();

      return calculateDynaviewId(facesContext, urlMapping);
   }
View Full Code Here

      {
         log.trace("Found @URLMapping annotation on class: " + clazz.getName());
      }

      // create UrlMapping from annotation
      UrlMapping mapping = new UrlMapping();
      mapping.setId(mappingAnnotation.id());
      mapping.setParentId(mappingAnnotation.parentId());
      mapping.setPattern(mappingAnnotation.pattern());
      mapping.setViewId(mappingAnnotation.viewId());
      mapping.setOutbound(mappingAnnotation.outbound());
      mapping.setOnPostback(mappingAnnotation.onPostback());

      // register mapping
      Object existingMapping = urlMappings.put(mapping.getId(), mapping);

      // fail if a mapping with this ID already existed
      if (existingMapping != null)
      {
         throw new IllegalArgumentException("Duplicated mapping id: " + mapping.getId());
      }

      // At bean name to lookup map if it has been specified
      if ((mappingAnnotation.beanName() != null) && (mappingAnnotation.beanName().length() > 0))
      {
         beanNameMap.put(clazz, mappingAnnotation.beanName());
      }

      // process validations
      for (URLValidator validationAnnotation : mappingAnnotation.validation())
      {

         // index attribute is required in this case
         if (validationAnnotation.index() < 0)
         {
            throw new IllegalArgumentException(
                     "Please set the index of the path parameter you want to validate with the @URLValidator specified on mapping: "
                              + mapping.getId());
         }

         // prepare PathValidator
         PathValidator pathValidator = new PathValidator();
         pathValidator.setIndex(validationAnnotation.index());
         pathValidator.setOnError(validationAnnotation.onError());
         pathValidator.setValidatorIds(join(validationAnnotation.validatorIds(), " "));

         // optional validator method
         if (!isBlank(validationAnnotation.validator()))
         {
            pathValidator.setValidatorExpression(new ConstantExpression(validationAnnotation.validator()));
         }

         // add PathValidator to the mapping
         mapping.getPathValidators().add(pathValidator);

      }

      // return mapping id
      return mapping.getId().trim();

   }
View Full Code Here

      {
         /*
          * FIXME this will not work with dynamic view IDs... figure out another solution
          * (<rewrite-view>/faces/views/myview.xhtml</rewrite-view> ? For now. Do not support it.
          */
         UrlMapping mapping = config.getMappingById(outcome);
         String viewId = mapping.getViewId();
         if (mapping.isDynaView())
         {
            viewId = dynaview.calculateDynaviewId(context, mapping);
         }
         viewId = FacesNavigationURLCanonicalizer.normalizeRequestURI(context, viewId);

         URL url = new URL(viewId);
         url.getMetadata().setLeadingSlash(true);
         QueryString qs = QueryString.build("");
         if (viewId.contains("?"))
         {
            qs.addParameters(viewId);
         }
         qs.addParameters("?" + PrettyFacesWrappedResponse.REWRITE_MAPPING_ID_KEY + "=" + mapping.getId());

         viewId = url.toString() + qs.toQueryString();

         NavigationCase navigationCase = parent.getNavigationCase(context, fromAction, viewId);
         return navigationCase;
View Full Code Here

   private void createAncestry(PrettyConfig config, UrlMapping m)
   {
      if (m.hasParent() && !seen.contains(m))
      {
         UrlMapping parent = config.getMappingById(m.getParentId());
         if (parent == null)
         {
            throw new PrettyException("Error when building configuration for URL-mapping [" + m.getId() + ":"
                     + m.getPattern() + "] - the requested parentId [" + m.getParentId()
                     + "] does not exist in the configuration.");
         }
         if (parent.hasParent())
         {
            createAncestry(config, parent);
         }
         m.setPattern(parent.getPattern() + m.getPattern());
         mergeValidators(parent, m);
         mergeActions(parent, m);
         mergeQueryParams(parent, m);
         seen.add(m);
      }
View Full Code Here

         // create an action for each referenced mapping
         for (String mappingId : actionSpec.getMappingIds())
         {

            // Get the mapping references by the action
            UrlMapping mapping = urlMappings.get(mappingId);

            /*
             * Fail for unresolved mappings. This may happen when the user places
             * invalid mapping IDs in the mappingId attribute of
             *
             * @URLAction or @URLQueryParameter
             */
            if (mapping == null)
            {
               throw new IllegalArgumentException("Unable to find the mapping '" + mappingId
                        + "' referenced at method '" + actionSpec.getMethod().getName() + "' in class '"
                        + actionSpec.getMethod().getDeclaringClass().getName() + "'.");
            }

            // build UrlMapping
            UrlAction urlAction = new UrlAction();
            urlAction.setPhaseId(actionSpec.getPhaseId());
            urlAction.setOnPostback(actionSpec.isOnPostback());
            urlAction.setInheritable(actionSpec.isInheritable());

            // try to get bean name
            Class<?> clazz = actionSpec.getMethod().getDeclaringClass();

            // build expression
            PrettyExpression expression = buildPrettyExpression(clazz, actionSpec.getMethod().getName());
            urlAction.setAction(expression);

            // trace
            if (log.isTraceEnabled())
            {
               log.trace("Adding action expression '" + urlAction.getAction() + "' to mapping: " + mapping.getId());
            }

            // register this action
            mapping.addAction(urlAction);

         }
      }

      for (QueryParamSpec queryParamSpec : queryParamList)
      {

         // create a query param for each referenced mapping
         for (String mappingId : queryParamSpec.getMappingIds())
         {

            // Get the mapping references by the query param
            UrlMapping mapping = urlMappings.get(mappingId);

            // fail for unresolved mappings
            if (mapping == null)
            {
               throw new IllegalArgumentException("Unable to find the mapping '" + mappingId
                        + "' referenced at field '" + queryParamSpec.getFieldName() + "' in class '"
                        + queryParamSpec.getOwnerClass().getName() + "'.");
            }

            // build UrlMapping
            QueryParameter queryParam = new QueryParameter();
            queryParam.setName(queryParamSpec.getName());
            queryParam.setOnError(queryParamSpec.getOnError());
            queryParam.setValidatorIds(join(queryParamSpec.getValidatorIds(), " "));
            queryParam.setOnPostback(queryParamSpec.isOnPostback());

            // optional validator method
            if (!isBlank(queryParamSpec.getValidator()))
            {
               queryParam.setValidatorExpression(new ConstantExpression(queryParamSpec.getValidator()));
            }

            // try to get bean name
            Class<?> clazz = queryParamSpec.getOwnerClass();

            // build expression
            PrettyExpression expression = buildPrettyExpression(clazz, queryParamSpec.getFieldName());
            queryParam.setExpression(expression);

            // trace
            if (log.isTraceEnabled())
            {
               log.trace("Registered query-param '" + queryParam.getName() + "' to '" + expression + "' in mapping: "
                        + mapping.getId());
            }

            // register this action
            mapping.addQueryParam(queryParam);

         }

      }

View Full Code Here

TOP

Related Classes of com.ocpsoft.pretty.faces.config.mapping.UrlMapping

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.