Package play.mvc.Http.MultipartFormData

Examples of play.mvc.Http.MultipartFormData.FilePart


    @BodyParser.Of(BodyParser.MultipartFormData.class)
    public Result create() {
        String path = getRefererPath();
        MultipartFormData body = request().body().asMultipartFormData();
        FilePart bundle = body.getFile("bundle");
        if (bundle != null) {
            CreateBundleRequest cbr;
            try {
                File file = bundle.getFile();
                String bundleContents = Files.toString(file, StandardCharsets.UTF_8);
                cbr = Json.fromJson(Json.parse(bundleContents), CreateBundleRequest.class);
            } catch (IOException e) {
                Logger.error("Could not parse uploaded bundle: " + e);
                flash("error", "The uploaded bundle could not be applied: does it have the right format?");
View Full Code Here


    @Override
    protected Result doExecute() throws DAOException, PartakeException {
        UserEx user = ensureLogin();
        ensureValidSessionToken();

        FilePart filePart = request().body().asMultipartFormData().getFile("file");
        if (filePart == null)
            return renderInvalid(UserErrorCode.INVALID_NOIMAGE);

        File file = filePart.getFile();
        String contentType = filePart.getContentType();

        if (file == null)
            return renderInvalid(UserErrorCode.INVALID_NOIMAGE);
        if (contentType == null)
            return renderInvalid(UserErrorCode.INVALID_IMAGE_CONTENTTYPE);
View Full Code Here

    @With({RootCredentialWrapFilter.class,ConnectToDBFilter.class})
    public static Result importDb(){
      String appcode = (String)ctx().args.get("appcode");
      MultipartFormData  body = request().body().asMultipartFormData();
      if (body==null) return badRequest("missing data: is the body multipart/form-data?");
      FilePart fp = body.getFile("file");

      if (fp!=null){
        ZipInputStream zis = null;
        try{
          java.io.File multipartFile=fp.getFile();
          java.util.UUID uuid = java.util.UUID.randomUUID();
          File zipFile = File.createTempFile(uuid.toString(), ".zip");
          FileUtils.copyFile(multipartFile,zipFile);
          zis =   new ZipInputStream(new FileInputStream(zipFile));
          DbManagerService.importDb(appcode, zis);
View Full Code Here

       
        IProperties i = (IProperties)PropertiesConfigurationHelper.findByKey(conf, key);
        if(i.getType().equals(ConfigurationFileContainer.class)){
          MultipartFormData  body = request().body().asMultipartFormData();
          if (body==null) return badRequest("missing data: is the body multipart/form-data?");
          FilePart file = body.getFile("file");
          if(file==null) return badRequest("missing file");
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          try{
            FileUtils.copyFile(file.getFile(),baos);
            Object fileValue = new ConfigurationFileContainer(file.getFilename(), baos.toByteArray());
            baos.close();
            conf.getMethod("setValue",Object.class).invoke(i,fileValue);
          }catch(Exception e){
            internalServerError(e.getMessage());
          }
View Full Code Here

    if(appcode == null || StringUtils.isEmpty(appcode.trim())){
      unauthorized("appcode can not be null");
    }
    MultipartFormData  body = request().body().asMultipartFormData();
    if (body==null) return badRequest("missing data: is the body multipart/form-data?");
    FilePart fp = body.getFile("file");

    if (fp!=null){
      ZipInputStream zis = null;
      try{
        java.io.File multipartFile=fp.getFile();
        java.util.UUID uuid = java.util.UUID.randomUUID();
        File zipFile = File.createTempFile(uuid.toString(), ".zip");
        FileUtils.copyFile(multipartFile,zipFile);
        zis =   new ZipInputStream(new FileInputStream(zipFile));
        DbManagerService.importDb(appcode, zis);
View Full Code Here

    public static Result storeFile() throws  Throwable {
      MultipartFormData  body = request().body().asMultipartFormData();
      if (body==null) return badRequest("missing data: is the body multipart/form-data? Check if it contains boundaries too! " );
      //FilePart file = body.getFile(FILE_FIELD_NAME);
      List<FilePart> files = body.getFiles();
      FilePart file = null;
      if (!files.isEmpty()) file = files.get(0);
      String ret="";
      if (file!=null){
        Map<String, String[]> data=body.asFormUrlEncoded();
        String[] datas=data.get(DATA_FIELD_NAME);
        String[] acl=data.get(ACL_FIELD_NAME);

        /*extract attachedData */
        String dataJson=null;
        if (datas!=null && datas.length>0){
          dataJson = datas[0];
        }else dataJson="{}";
       
        /*extract acl*/
        /*the acl json must have the following format:
         * {
         *     "read" : {
         *           "users":[],
         *           "roles":[]
         *          }
         *     "update" : .......
         * }
         */
        String aclJsonString=null;
        if (acl!=null && datas.length>0){
          aclJsonString = acl[0];
          ObjectMapper mapper = new ObjectMapper();
          JsonNode aclJson=null;
          try{
            aclJson = mapper.readTree(aclJsonString);
          }catch(JsonProcessingException e){
            return status(CustomHttpCode.ACL_JSON_FIELD_MALFORMED.getBbCode(),"The 'acl' field is malformed");
          }
          /*check if the roles and users are valid*/
           Iterator<Entry<String, JsonNode>> it = aclJson.fields();
           while (it.hasNext()){
             //check for permission read/update/delete/all
             Entry<String, JsonNode> next = it.next();
             if (!PermissionsHelper.permissionsFromString.containsKey(next.getKey())){
               return status(CustomHttpCode.ACL_PERMISSION_UNKNOWN.getBbCode(),"The key '"+next.getKey()+"' is invalid. Valid ones are 'read','update','delete','all'");
             }
             //check for users/roles
             Iterator<Entry<String, JsonNode>> it2 = next.getValue().fields();
             while (it2.hasNext()){
               Entry<String, JsonNode> next2 = it2.next();
               if (!next2.getKey().equals("users") && !next2.getKey().equals("roles")) {
                 return status(CustomHttpCode.ACL_USER_OR_ROLE_KEY_UNKNOWN.getBbCode(),"The key '"+next2.getKey()+"' is invalid. Valid ones are 'users' or 'roles'");
               }
               //check for the existance of users/roles
               JsonNode arrNode = next2.getValue();
               if (arrNode.isArray()) {
                    for (final JsonNode objNode : arrNode) {
                        //checks the existance users and/or roles
                      if (next2.getKey().equals("users") && !UserService.exists(objNode.asText())) return status(CustomHttpCode.ACL_USER_DOES_NOT_EXIST.getBbCode(),"The user " + objNode.asText() + " does not exists");
                      if (next2.getKey().equals("roles") && !RoleService.exists(objNode.asText())) return status(CustomHttpCode.ACL_ROLE_DOES_NOT_EXIST.getBbCode(),"The role " + objNode.asText() + " does not exists");
                     
                    }
               }else return status(CustomHttpCode.JSON_VALUE_MUST_BE_ARRAY.getBbCode(),"The '"+next2.getKey()+"' value must be an array");
             }
            
           }
         
        }else aclJsonString="{}";
       
       
       
          java.io.File fileContent=file.getFile();
        String fileName = file.getFilename();
         /*String contentType = file.getContentType();
          if (contentType==null || contentType.isEmpty() || contentType.equalsIgnoreCase("application/octet-stream")){  //try to guess the content type
            InputStream is = new BufferedInputStream(new FileInputStream(fileContent));
            contentType = URLConnection.guessContentTypeFromStream(is);
            if (contentType==null || contentType.isEmpty()) contentType="application/octet-stream";
View Full Code Here

 
 
  private static Result postFile() throws  Throwable{
    MultipartFormData  body = request().body().asMultipartFormData();
    if (body==null) return badRequest("missing data: is the body multipart/form-data? Check if it contains boundaries too!");
    FilePart file = body.getFile("file");
    Map<String, String[]> data=body.asFormUrlEncoded();
    String[] meta=data.get("meta");
    String[] name=data.get("name");
    if (name==null || name.length==0 || StringUtils.isEmpty(name[0].trim())) return badRequest("missing name field");
    String ret="";
    if (file!=null){
      String metaJson=null;
      if (meta!=null && meta.length>0){
        metaJson = meta[0];
      }
        java.io.File fileContent=file.getFile();
        byte [] fileContentAsByteArray=Files.toByteArray(fileContent);
      String fileName = file.getFilename();
        String contentType = file.getContentType();
        if (contentType==null || contentType.isEmpty() || contentType.equalsIgnoreCase("application/octet-stream")){  //try to guess the content type
          InputStream is = new BufferedInputStream(new FileInputStream(fileContent));
          contentType = URLConnection.guessContentTypeFromStream(is);
          if (contentType==null || contentType.isEmpty()) contentType="application/octet-stream";
        }
View Full Code Here

    public static final long TEMPORARYFILES_KEEPUP_TIME_MILLIS = Configuration.root()
            .getMilliseconds("application.temporaryfiles.keep-up.time", 24 * 60 * 60 * 1000L);

    public static Result uploadFile() throws NoSuchAlgorithmException, IOException {
        // Get the file from request.
        FilePart filePart =
                request().body().asMultipartFormData().getFile("filePath");
        if (filePart == null) {
            return badRequest();
        }
        File file = filePart.getFile();

        User uploader = UserApp.currentUser();

        // Anonymous cannot upload a file.
        if (uploader.isAnonymous()) {
            return forbidden();
        }

        // Attach the file to the user who upload it.
        Attachment attach = new Attachment();
        boolean isCreated = attach.store(file, filePart.getFilename(), uploader.asResource());

        // The request has been fulfilled and resulted in a new resource being
        // created. The newly created resource can be referenced by the URI(s)
        // returned in the entity of the response, with the most specific URI
        // for the resource given by a Location header field.
View Full Code Here

                    filledUpdatedProjectForm, project, repository.getBranches()));
        }

        Project updatedProject = filledUpdatedProjectForm.get();

        FilePart filePart = request().body().asMultipartFormData().getFile("logoPath");

        if (!isEmptyFilePart(filePart)) {
            Attachment.deleteAll(updatedProject.asResource());
            new Attachment().store(filePart.getFile(), filePart.getFilename(), updatedProject.asResource());
        }

        Map<String, String[]> data = request().body().asMultipartFormData().asFormUrlEncoded();
        String defaultBranch = HttpUtil.getFirstValueFromQuery(data, "defaultBranch");
        if (StringUtils.isNotEmpty(defaultBranch)) {
View Full Code Here

        if (!Project.projectNameChangeable(id, loginId, name)) {
            flash(Constants.WARNING, "project.name.duplicate");
            updateProjectForm.reject("name", "project.name.duplicate");
        }

        FilePart filePart = request().body().asMultipartFormData().getFile("logoPath");

        if (!isEmptyFilePart(filePart)) {
            if (!isImageFile(filePart.getFilename())) {
                flash(Constants.WARNING, "project.logo.alert");
                updateProjectForm.reject("logoPath", "project.logo.alert");
            } else if (filePart.getFile().length() > LOGO_FILE_LIMIT_SIZE) {
                flash(Constants.WARNING, "project.logo.fileSizeAlert");
                updateProjectForm.reject("logoPath", "project.logo.fileSizeAlert");
            }
        }
View Full Code Here

TOP

Related Classes of play.mvc.Http.MultipartFormData.FilePart

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.