Save This Page
Home » tapestry-src-5.0.19 » org.apache.tapestry5.upload.components » [javadoc | source]
    1   // Copyright 2007, 2008 The Apache Software Foundation
    2   //
    3   // Licensed under the Apache License, Version 2.0 (the "License");
    4   // you may not use this file except in compliance with the License.
    5   // You may obtain a copy of the License at
    6   //
    7   //     http://www.apache.org/licenses/LICENSE-2.0
    8   //
    9   // Unless required by applicable law or agreed to in writing, software
   10   // distributed under the License is distributed on an "AS IS" BASIS,
   11   // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12   // See the License for the specific language governing permissions and
   13   // limitations under the License.
   14   
   15   package org.apache.tapestry5.upload.components;
   16   
   17   import org.apache.tapestry5;
   18   import org.apache.tapestry5.annotations.Environmental;
   19   import org.apache.tapestry5.annotations.Mixin;
   20   import org.apache.tapestry5.annotations.Parameter;
   21   import org.apache.tapestry5.annotations.Path;
   22   import org.apache.tapestry5.corelib.base.AbstractField;
   23   import org.apache.tapestry5.corelib.mixins.RenderDisabled;
   24   import org.apache.tapestry5.ioc.annotations.Inject;
   25   import org.apache.tapestry5.services.ComponentDefaultProvider;
   26   import org.apache.tapestry5.services.FieldValidatorDefaultSource;
   27   import org.apache.tapestry5.services.FormSupport;
   28   import org.apache.tapestry5.services.Request;
   29   import org.apache.tapestry5.upload.services.MultipartDecoder;
   30   import org.apache.tapestry5.upload.services.UploadedFile;
   31   
   32   import java.util.Locale;
   33   
   34   /**
   35    * A component to upload a file.
   36    */
   37   @SuppressWarnings({"UnusedDeclaration"})
   38   public class Upload extends AbstractField
   39   {
   40       public static final String MULTIPART_ENCTYPE = "multipart/form-data";
   41   
   42       /**
   43        * The uploaded file. Note: This is only guaranteed to be valid while processing the form submission. Subsequently
   44        * the content may have been cleaned up.
   45        */
   46       @Parameter(required = true, principal = true, autoconnect = true)
   47       private UploadedFile value;
   48   
   49       /**
   50        * The object that will perform input validation. The "validate:" binding prefix is generally used to provide this
   51        * object in a declarative fashion.
   52        */
   53       @Parameter(defaultPrefix = BindingConstants.VALIDATE)
   54       @SuppressWarnings("unchecked")
   55       private FieldValidator<Object> validate;
   56   
   57       @Environmental
   58       private ValidationTracker tracker;
   59   
   60       @Inject
   61       private MultipartDecoder decoder;
   62   
   63       @Environmental
   64       private FormSupport formSupport;
   65   
   66       @Inject
   67       private ComponentDefaultProvider defaultProvider;
   68   
   69       @Inject
   70       private ComponentResources resources;
   71   
   72       @Inject
   73       private Locale locale;
   74   
   75       @Inject
   76       private FieldValidationSupport fieldValidationSupport;
   77   
   78       @SuppressWarnings("unused")
   79       @Mixin
   80       private RenderDisabled renderDisabled;
   81   
   82       @Inject
   83       @Path("upload.js")
   84       private Asset uploadScript;
   85   
   86       @Inject
   87       private Request request;
   88   
   89       @Environmental
   90       private RenderSupport renderSupport;
   91   
   92       /**
   93        * Computes a default value for the "validate" parameter using {@link FieldValidatorDefaultSource}.
   94        */
   95       final Binding defaultValidate()
   96       {
   97           return defaultProvider.defaultValidatorBinding("value", resources);
   98       }
   99   
  100       public Upload()
  101       {
  102       }
  103   
  104       // For testing
  105       Upload(UploadedFile value, FieldValidator<Object> validate, MultipartDecoder decoder, ValidationTracker tracker,
  106              ComponentResources resources, FieldValidationSupport fieldValidationSupport)
  107       {
  108           this.value = value;
  109           if (validate != null) this.validate = validate;
  110           this.decoder = decoder;
  111           this.tracker = tracker;
  112           this.resources = resources;
  113           this.fieldValidationSupport = fieldValidationSupport;
  114       }
  115   
  116       @SuppressWarnings({"unchecked"})
  117       @Override
  118       protected void processSubmission(String elementName)
  119       {
  120           UploadedFile uploaded = decoder.getFileUpload(elementName);
  121   
  122           if (uploaded != null)
  123           {
  124               if (uploaded.getFileName() == null || uploaded.getFileName().length() == 0) uploaded = null;
  125           }
  126   
  127           try
  128           {
  129               fieldValidationSupport.validate(uploaded, resources, validate);
  130           }
  131           catch (ValidationException ex)
  132           {
  133               tracker.recordError(this, ex.getMessage());
  134           }
  135   
  136           value = uploaded;
  137       }
  138   
  139       /**
  140        * Render the upload tags.
  141        *
  142        * @param writer Writer to output markup
  143        */
  144       protected void beginRender(MarkupWriter writer)
  145       {
  146           formSupport.setEncodingType(MULTIPART_ENCTYPE);
  147   
  148           writer.element("input", "type", "file", "name", getControlName(), "id", getClientId());
  149   
  150           validate.render(writer);
  151   
  152           resources.renderInformalParameters(writer);
  153   
  154           decorateInsideField();
  155   
  156           // TAPESTRY-2453
  157           if (request.isXHR())
  158           {
  159               renderSupport.addScriptLink(uploadScript);
  160               renderSupport.addInit("injectedUpload", getClientId());
  161           }
  162       }
  163   
  164       public void afterRender(MarkupWriter writer)
  165       {
  166           writer.end();
  167       }
  168   
  169       public UploadedFile getValue()
  170       {
  171           return value;
  172       }
  173   
  174       Upload injectDecorator(ValidationDecorator decorator)
  175       {
  176           setDecorator(decorator);
  177   
  178           return this;
  179       }
  180   
  181       Upload injectRequest(Request request)
  182       {
  183           this.request = request;
  184   
  185           return this;
  186       }
  187   
  188       Upload injectFormSupport(FormSupport formSupport)
  189       {
  190           // We have our copy ...
  191           this.formSupport = formSupport;
  192   
  193           // As does AbstractField
  194           setFormSupport(formSupport);
  195   
  196           return this;
  197       }
  198   
  199       Upload injectFieldValidator(FieldValidator validator)
  200       {
  201           this.validate = validator;
  202   
  203           return this;
  204       }
  205   }

Save This Page
Home » tapestry-src-5.0.19 » org.apache.tapestry5.upload.components » [javadoc | source]