Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.analogweb.acf;
  
  import static org.analogweb.acf.CommonsFileUploadModulesConfig.PLUGIN_MESSAGE_RESOURCE;
  
  import java.io.File;
 import java.util.List;
 
 
 
     private static final Log log = Logs.getLog(MultipartParameterResolver.class);
     private FileUploadFactory<? extends FileUploadfileUploadFactory = new DefaultFileUploadFactory();
     private final String defaultEncoding = "UTF-8";
 
     @Override
     public boolean supports(MediaType mediaType) {
         return ..isCompatible(mediaType);
     }
 
     @Override
     public Object resolveValue(RequestContext requestInvocationMetadata metadataString name,
             Class<?> requiredTypeAnnotation[] annotations) {
         MultipartParameters<Multipartparameters = CurrentMultipartParameters.get(request);
         if (parameters == null) {
             if (isMultipartContentOnCurrentRequest(request)) {
                 .log("DACF000001");
                 final FileUpload fileUpload = getFileUpload(getFileItemFactory());
                 .log("DACF000002"new Object[] { fileUpload });
                 final String encoding = resolveEncoding(request);
                 .log("DACF000003"new Object[] { encoding });
                 try {
                     parameters = createMultipartParameters(requestcreateRequestContext(request),
                             fileUploadencoding);
                     CurrentMultipartParameters.put(requestparameters);
                 } catch (final FileUploadException e) {
                     throw new FileUploadFailureException(e);
                 } catch (final IOException e) {
                     throw new FileUploadFailureException(e);
                 }
             } else {
                 .log("DACF000009",
                         new Object[] { request.getContentType() });
                 return super.resolveValue(requestmetadatanamerequiredTypeannotations);
             }
         }
         .log("TACF000009"new Object[] { parametersname,
                 requiredType });
         if (isEqualsType(Iterable.classrequiredType)) {
             return parameters;
         }
         return resolveParameterizedValue(requestmetadatanamerequiredTypeannotations,
                 parameters);
     }
 
     protected Object resolveParameterizedValue(RequestContext requestInvocationMetadata metadata,
             String nameClass<?> requiredTypeAnnotation[] annotations,
             MultipartParameters<Multipartparameters) {
         final Multipart[] value = parameters.getMultiparts(name);
         if (ArrayUtils.isNotEmpty(value)) {
             .log("TACF000004"new Object[] { namevalue });
             if (isEqualsType(ClassUtils.forNameQuietly("[L" + File.class.getName() + ";"),
                     requiredType)) {
                 final List<Filefiles = new ArrayList<File>();
                 for (final Multipart mp : value) {
                     File f;
                     if (mp instanceof FileItemMultipart
                             && (f = ((FileItemMultipartmp).getAsTemporalyFile()) != null) {
                         files.add(f);
                     }
                 }
                 return files.toArray(new File[files.size()]);
             } else if (isEqualsType(
                     ClassUtils.forNameQuietly("[L" + Multipart.class.getName() + ";"), requiredType)) {
                 return value;
             }
            final Multipart mp = value[0];
            if (isEqualsType(InputStream.classrequiredType)) {
                return mp.getInputStream();
            } else if (isEqualsType(File.classrequiredType)) {
                File f;
                if (mp instanceof FileItemMultipart
                        && (f = ((FileItemMultipartmp).getAsTemporalyFile()) != null) {
                    return f;
                }
            } else if (isEqualsType(byte[].classrequiredType)) {
                return mp.getBytes();
            } else if (isEqualsType(Multipart.classrequiredType)) {
                return mp;
            } else {
                throw new UnsupportedParameterTypeException(namerequiredType);
            }
        }
        return super.resolveValue(requestmetadatanamerequiredTypeannotations);
    }
    protected boolean isMultipartContentOnCurrentRequest(RequestContext request) {
        final String method = request.getRequestMethod();
        if (StringUtils.isEmpty(method) || method.equalsIgnoreCase("POST") == false) {
            return false;
        }
        final MediaType type = request.getContentType();
        if (type == null) {
            return false;
        }
        return type.toString().startsWith("multipart/");
    }
    protected String resolveEncoding(RequestContext request) {
        final String encoding = request.getCharacterEncoding();
        if (StringUtils.isNotEmpty(encoding)) {
            return encoding;
        }
        return this.;
    }
    @SuppressWarnings("unchecked")
    protected <T extends MultipartMultipartParameters<T> createMultipartParameters(
            RequestContext requestorg.apache.commons.fileupload.RequestContext context,
            FileUpload fileUploadString resolvedEncodingthrows FileUploadExceptionIOException {
        final List<FileItemfileItems = fileUpload.parseRequest(context);
        .log("TACF000003"fileItems.size());
        return (MultipartParameters<T>) new FileItemMultipartParameters(fileItemsresolvedEncoding);
    }
            RequestContext request) {
        return new AnalogwebRequestContext(request);
    }
    protected FileItemFactory getFileItemFactory() {
        return this.;
    }
    protected FileItemFactory createFileItemFactory() {
        File tmpDir = ApplicationPropertiesHolder.current()
                .getTempDir();
        if (tmpDir.exists() == false) {
            try {
                boolean created = tmpDir.mkdirs();
                if (created == false) {
                    .log(."WACF000003",
                            tmpDir.getPath());
                }
            } catch (SecurityException e) {
                .log(."WACF000003"e,
                        tmpDir.getPath());
            }
        }
        final DiskFileItemFactory factory = new DiskFileItemFactory(
                .tmpDir);
        factory.setFileCleaningTracker(new FileCleaningTracker());
        return factory;
    }
    protected FileUpload getFileUpload(FileItemFactory fileItemFactory) {
        if (fileItemFactory != null) {
            return .createFileUpload(fileItemFactory);
        } else {
            return .createFileUpload();
        }
    }
    private boolean isEqualsType(Class<?> clazzClass<?> other) {
        if (clazz == null || other == null) {
            return false;
        }
        return (clazz == other) || clazz.getCanonicalName().equals(other.getCanonicalName());
    }
    public void setFileItemFactory(FileItemFactory fileItemFactory) {
        this. = fileItemFactory;
    }
    public void setFileUploadFactory(FileUploadFactory<? extends FileUploadfileUploadFactory) {
        this. = fileUploadFactory;
    }
New to GrepCode? Check out our FAQ X