@Put
  public AttachmentResponse addAttachment(Representation entity) {
    if (authenticate() == false) return null;

    String taskId = (String) getRequest().getAttributes().get("taskId");

    try {
      RestletFileUpload upload = new RestletFileUpload(new DiskFileItemFactory());
      List<FileItem> items = upload.parseRepresentation(entity);

      FileItem uploadItem = null;
      for (FileItem fileItem : items) {
        if (fileItem.getName() != null) {
          uploadItem = fileItem;
        }
      }

      String fileName = uploadItem.getName();

      Attachment attachment =
          ActivitiUtil.getTaskService()
              .createAttachment(
                  uploadItem.getContentType(),
                  taskId,
                  null,
                  fileName,
                  fileName,
                  uploadItem.getInputStream());

      return new AttachmentResponse(attachment);

    } catch (Exception e) {
      throw new ActivitiException("Unable to add new attachment to task " + taskId);
    }
  }
  protected AttachmentResponse createBinaryAttachment(Representation representation, Task task)
      throws FileUploadException, IOException {
    RestletFileUpload upload = new RestletFileUpload(new DiskFileItemFactory());
    List<FileItem> items = upload.parseRepresentation(representation);

    String name = null;
    String description = null;
    String type = null;
    FileItem uploadItem = null;

    for (FileItem fileItem : items) {
      if (fileItem.isFormField()) {
        if ("name".equals(fileItem.getFieldName())) {
          name = fileItem.getString("UTF-8");
        } else if ("description".equals(fileItem.getFieldName())) {
          description = fileItem.getString("UTF-8");
        } else if ("type".equals(fileItem.getFieldName())) {
          type = fileItem.getString("UTF-8");
        }
      } else if (fileItem.getName() != null) {
        uploadItem = fileItem;
      }
    }

    if (name == null) {
      throw new ActivitiIllegalArgumentException("Attachment name is required.");
    }

    if (uploadItem == null) {
      throw new ActivitiIllegalArgumentException("Attachment content is required.");
    }

    Attachment createdAttachment =
        ActivitiUtil.getTaskService()
            .createAttachment(
                type, task.getId(), null, name, description, uploadItem.getInputStream());

    setStatus(Status.SUCCESS_CREATED);
    return getApplication(ActivitiRestServicesApplication.class)
        .getRestResponseFactory()
        .createAttachmentResponse(this, createdAttachment);
  }
  public void upload(Representation representation) throws ResourceException {
    Object result = null;

    List<FileItem> files = null;

    try {
      RestletFileUpload uploadRequest = new RestletFileUpload(getFileItemFactory());

      files = uploadRequest.parseRepresentation(representation);

      result = delegate.upload(getContext(), getRequest(), getResponse(), files);
    } catch (FileUploadException e) {
      // try to take simply the body as stream
      String name = getRequest().getResourceRef().getPath();

      if (name.contains("/")) {
        name = name.substring(name.lastIndexOf("/") + 1, name.length());
      }

      FileItem file = new FakeFileItem(name, representation);

      files = new ArrayList<FileItem>();

      files.add(file);

      result = delegate.upload(getContext(), getRequest(), getResponse(), files);
    }

    // only if the status was not changed to be something else, like a 202
    if (getResponse().getStatus() == Status.SUCCESS_OK) {
      getResponse().setStatus(Status.SUCCESS_CREATED);
    }

    if (result != null) {
      // TODO: representation cannot be returned as multipart! (representation above is possibly
      // multipart)
      getResponse().setEntity(doRepresent(result, getPreferredVariant(), getResponse()));
    }
  }
  protected RestVariable setBinaryVariable(
      Representation representation, Task task, boolean isNew) {
    try {
      RestletFileUpload upload = new RestletFileUpload(new DiskFileItemFactory());
      List<FileItem> items = upload.parseRepresentation(representation);

      String variableScope = null;
      String variableName = null;
      String variableType = null;
      FileItem uploadItem = null;

      for (FileItem fileItem : items) {
        if (fileItem.isFormField()) {
          if ("scope".equals(fileItem.getFieldName())) {
            variableScope = fileItem.getString("UTF-8");
          } else if ("name".equals(fileItem.getFieldName())) {
            variableName = fileItem.getString("UTF-8");
          } else if ("type".equals(fileItem.getFieldName())) {
            variableType = fileItem.getString("UTF-8");
          }
        } else if (fileItem.getName() != null) {
          uploadItem = fileItem;
        }
      }

      // Validate input and set defaults
      if (uploadItem == null) {
        throw new ActivitiIllegalArgumentException("No file content was found in request body.");
      }

      if (variableName == null) {
        throw new ActivitiIllegalArgumentException("No variable name was found in request body.");
      }

      if (variableType != null) {
        if (!RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE.equals(variableType)
            && !RestResponseFactory.SERIALIZABLE_VARIABLE_TYPE.equals(variableType)) {
          throw new ActivitiIllegalArgumentException(
              "Only 'binary' and 'serializable' are supported as variable type.");
        }
      } else {
        variableType = RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE;
      }

      RestVariableScope scope = RestVariableScope.LOCAL;
      if (variableScope != null) {
        scope = RestVariable.getScopeFromString(variableScope);
      }

      if (variableType.equals(RestResponseFactory.BYTE_ARRAY_VARIABLE_TYPE)) {
        // Use raw bytes as variable value
        ByteArrayOutputStream variableOutput =
            new ByteArrayOutputStream(((Long) uploadItem.getSize()).intValue());
        IOUtils.copy(uploadItem.getInputStream(), variableOutput);
        setVariable(task, variableName, variableOutput.toByteArray(), scope, isNew);
      } else {
        // Try deserializing the object
        ObjectInputStream stream = new ObjectInputStream(uploadItem.getInputStream());
        Object value = stream.readObject();
        setVariable(task, variableName, value, scope, isNew);
        stream.close();
      }

      return getApplication(ActivitiRestServicesApplication.class)
          .getRestResponseFactory()
          .createBinaryRestVariable(
              this, variableName, scope, variableType, task.getId(), null, null);

    } catch (FileUploadException fue) {
      throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, fue);
    } catch (IOException ioe) {
      throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, ioe);
    } catch (ClassNotFoundException ioe) {
      throw new ResourceException(
          Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE.getCode(),
          "The provided body contains a serialized object for which the class is nog found: "
              + ioe.getMessage(),
          null,
          null);
    }
  }