play.libs.Files Java Examples

The following examples show how to use play.libs.Files. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BadgeIssuerController.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
/**
 * This method will read from input data and put all the requested key and value inside a map.
 *
 * @param body MultipartFormData
 * @param map Map<String,Object>
 * @return Map<String,Object>
 * @throws IOException
 */
private Map<String, Object> readFormData(MultipartFormData body, Map<String, Object> map)
    throws IOException {
  Map<String, String[]> data = body.asFormUrlEncoded();
  for (Entry<String, String[]> entry : data.entrySet()) {
    map.put(entry.getKey(), entry.getValue()[0]);
  }
  List<FilePart<Files.TemporaryFile>> filePart = body.getFiles();
  if (filePart != null && !filePart.isEmpty()) {
    File f = filePart.get(0).getRef().path().toFile();
    InputStream is = new FileInputStream(f);
    byte[] byteArray = IOUtils.toByteArray(is);
    map.put(JsonKey.FILE_NAME, filePart.get(0).getFilename());
    map.put(JsonKey.IMAGE, byteArray);
  }
  return map;
}
 
Example #2
Source File: FilesystemStore.java    From restcommander with Apache License 2.0 6 votes vote down vote up
public void rebuildAllIndexes() throws Exception {
    stop();
    File fl = new File(DATA_PATH);
    Files.deleteDirectory(fl);
    fl.mkdirs();
    List<ApplicationClass> classes = Play.classes.getAnnotatedClasses(Indexed.class);
    for (ApplicationClass applicationClass : classes) {
        List<JPABase> objects = JPA.em().createQuery(
                                        "select e from " + applicationClass.javaClass.getCanonicalName() + " as e")
                                        .getResultList();
        for (JPABase jpaBase : objects) {
            index(jpaBase, applicationClass.javaClass.getName());
        }
    }
    Logger.info("Rebuild index finished");
}
 
Example #3
Source File: FilesystemStore.java    From restcommander with Apache License 2.0 6 votes vote down vote up
public void delete(String name) {
    synchronized (this) {
        try {
            if (indexSearchers.containsKey(name)) {
                IndexReader rd = indexSearchers.get(name).getIndexReader();
                indexSearchers.get(name).close();
                indexSearchers.remove(name);
            }
            if (indexWriters.containsKey(name)) {
                indexWriters.get(name).close();
                indexWriters.remove(name);
            }
            File target = new File(DATA_PATH, name);
            if (target.exists() && target.isDirectory())
                Files.deleteDirectory(target);
        } catch (Exception e) {
            throw new UnexpectedException("Can't reopen reader", e);
        }
    }
}
 
Example #4
Source File: BadgeClassController.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
/**
 * Create a new badge class for a particular issuer.
 *
 * <p>Request body contains following parameters: issuerId: The ID of the Issuer to be owner of
 * the new Badge Class name: The name of the Badge Class description: A short description of the
 * new Badge Class. image: An image to represent the Badge Class. criteria: Either a text string
 * or a URL of a remotely hosted page describing the criteria rootOrgId: Root organisation ID
 * type: Badge class type (user / content) subtype: Badge class subtype (e.g. award) roles: JSON
 * array of roles (e.g. [ "OFFICIAL_TEXTBOOK_BADGE_ISSUER" ])
 *
 * @return Return a promise for create badge class API result.
 */
public CompletionStage<Result> createBadgeClass(Http.Request httpRequest) {
  ProjectLogger.log("createBadgeClass called", LoggerEnum.DEBUG.name());

  try {
    Request request = createAndInitRequest(BadgingActorOperations.CREATE_BADGE_CLASS.getValue(), httpRequest);

    HashMap<String, Object> map = new HashMap<>();

    Http.MultipartFormData multipartBody = httpRequest.body().asMultipartFormData();

    if (multipartBody != null) {
      Map<String, String[]> data = multipartBody.asFormUrlEncoded();
      for (Map.Entry<String, String[]> entry : data.entrySet()) {
        map.put(entry.getKey(), entry.getValue()[0]);
      }

      List<Http.MultipartFormData.FilePart<Files.TemporaryFile>> imageFilePart = multipartBody.getFiles();
      if (imageFilePart.size() > 0) {
        InputStream inputStream = new FileInputStream(imageFilePart.get(0).getRef().path().toFile());
        byte[] imageByteArray = IOUtils.toByteArray(inputStream);
        map.put(JsonKey.IMAGE, imageByteArray);
      }
    }

    request.setRequest(map);

    new BadgeClassValidator().validateCreateBadgeClass(request, Common.getRequestHeadersInArray(httpRequest.getHeaders().toMap()));

    return actorResponseHandler(getActorRef(), request, timeout, null, httpRequest);
  } catch (Exception e) {
    ProjectLogger.log("createBadgeClass: exception = ", e);

    return CompletableFuture.completedFuture(createCommonExceptionResponse(e, httpRequest));
  }
}
 
Example #5
Source File: FileUpload.java    From restcommander with Apache License 2.0 5 votes vote down vote up
public File asFile(File file) {
    try {
        Files.copy(defaultFile, file);
        return file;
    } catch (Exception ex) {
        throw new UnexpectedException(ex);
    }
}
 
Example #6
Source File: FileAttachment.java    From restcommander with Apache License 2.0 4 votes vote down vote up
void save() {
    if (f != null) {
        File to = new File(getStore(), model.getClass().getName() + "." + name + "_" + ((Model) model)._key());
        Files.copy(f, to);
    }
}