All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.resteasy.core.PrefixedFormInjector Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package org.jboss.resteasy.core;

import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.HttpResponse;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.jboss.resteasy.util.PrefixedFormFieldsHttpRequest;

import javax.ws.rs.core.MultivaluedMap;
import java.util.List;

/**
 * Extension of {@link FormInjector} that handles prefixes for associated classes.
 */
public class PrefixedFormInjector extends FormInjector
{

   private final String prefix;

   /**
    * Constructor setting the prefix.
    */
   public PrefixedFormInjector(Class type, String prefix, ResteasyProviderFactory factory)
   {
      super(type, factory);
      this.prefix = prefix;
   }

   /**
    * {@inheritDoc} Wraps the request in a
    */
   @Override
   public Object inject(HttpRequest request, HttpResponse response)
   {
      if (!containsPrefixedFormFieldsWithValue(request.getDecodedFormParameters()))
      {
         return null;
      }
      return doInject(prefix, request, response);
   }

   /**
    * Calls the super {@link #inject(org.jboss.resteasy.spi.HttpRequest, org.jboss.resteasy.spi.HttpResponse)} method.
    */
   protected Object doInject(String prefix, HttpRequest request, HttpResponse response)
   {
      return super.inject(new PrefixedFormFieldsHttpRequest(prefix, request), response);
   }

   /**
    * Checks to see if the decodedParameters contains any form fields starting with the prefix. Also checks if the value is not empty.
    */
   private boolean containsPrefixedFormFieldsWithValue(MultivaluedMap decodedFormParameters)
   {
      for (String parameterName : decodedFormParameters.keySet())
      {
         if (parameterName.startsWith(prefix))
         {
            if (hasValue(decodedFormParameters.get(parameterName)))
            {
               return true;
            }
         }
      }
      return false;
   }

   /**
    * Checks that the list has an non empty value.
    */
   protected boolean hasValue(List list)
   {
      return !list.isEmpty() && list.get(0).length() > 0;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy