/** * Creates a response for a variable of type {@link ValueType#FILE}. */ protected Response responseForFileVariable(FileValue fileValue) { String type = fileValue.getMimeType() != null ? fileValue.getMimeType() : MediaType.APPLICATION_OCTET_STREAM; if (fileValue.getEncoding() != null) { type += "; charset=" + fileValue.getEncoding(); } Object value = fileValue.getValue() == null ? "" : fileValue.getValue(); return Response.ok(value, type).header("Content-Disposition", "attachment; filename=" + fileValue.getFilename()).build(); }
/** * Creates a response for a variable of type {@link ValueType#FILE}. */ protected Response responseForFileVariable(FileValue fileValue) { String type = fileValue.getMimeType() != null ? fileValue.getMimeType() : MediaType.APPLICATION_OCTET_STREAM; if (fileValue.getEncoding() != null) { type += "; charset=" + fileValue.getEncoding(); } Object value = fileValue.getValue() == null ? "" : fileValue.getValue(); return Response.ok(value, type).header("Content-Disposition", "attachment; filename=" + fileValue.getFilename()).build(); }
@Override public Map<String, Object> getValueInfo(TypedValue typedValue) { if (!(typedValue instanceof FileValue)) { throw new IllegalArgumentException("Value not of type FileValue"); } FileValue fileValue = (FileValue) typedValue; Map<String, Object> result = new HashMap<String, Object>(2); result.put(VALUE_INFO_FILE_NAME, fileValue.getFilename()); if (fileValue.getMimeType() != null) { result.put(VALUE_INFO_FILE_MIME_TYPE, fileValue.getMimeType()); } if (fileValue.getEncoding() != null) { result.put(VALUE_INFO_FILE_ENCODING, fileValue.getEncoding()); } if (fileValue.isTransient()) { result.put(VALUE_INFO_TRANSIENT, fileValue.isTransient()); } return result; }
@Test public void testReadFilenameAndByteArrayValue() throws IOException { InputStream is = this.getClass().getClassLoader().getResourceAsStream("org/camunda/bpm/engine/test/standalone/variables/simpleFile.txt"); byte[] data = new byte[is.available()]; DataInputStream dataInputStream = new DataInputStream(is); dataInputStream.readFully(data); dataInputStream.close(); MockValueFields valueFields = new MockValueFields(); String filename = "file.txt"; valueFields.setTextValue(filename); valueFields.setByteArrayValue(data); FileValue fileValue = serializer.readValue(valueFields, true); assertThat(fileValue.getFilename(), is(filename)); assertThat(fileValue.getMimeType(), is(nullValue())); checkStreamFromValue(fileValue, "text"); }
@Test public void testReadFilenameValue() throws IOException { MockValueFields valueFields = new MockValueFields(); String filename = "file.txt"; valueFields.setTextValue(filename); FileValue fileValue = serializer.readValue(valueFields, true); assertThat(fileValue.getFilename(), is(filename)); assertThat(fileValue.getMimeType(), is(nullValue())); assertThat(fileValue.getValue(), is(nullValue())); }
protected FileValue fileValueWithDecodedString(FileValue fileValue, String value) { return Variables.fileValue(fileValue.getFilename()) .file(Base64.decodeBase64(value)) .mimeType(fileValue.getMimeType()) .encoding(fileValue.getEncoding()) .create(); }
@Test public void testReadFileNameEncodingAndByteArray() throws IOException { InputStream is = this.getClass().getClassLoader().getResourceAsStream("org/camunda/bpm/engine/test/standalone/variables/simpleFile.txt"); byte[] data = new byte[is.available()]; DataInputStream dataInputStream = new DataInputStream(is); dataInputStream.readFully(data); dataInputStream.close(); MockValueFields valueFields = new MockValueFields(); String filename = "file.txt"; valueFields.setTextValue(filename); valueFields.setByteArrayValue(data); String encoding = SEPARATOR + "UTF-8"; valueFields.setTextValue2(encoding); FileValue fileValue = serializer.readValue(valueFields, true); assertThat(fileValue.getFilename(), is(filename)); assertThat(fileValue.getEncoding(), is("UTF-8")); assertThat(fileValue.getEncodingAsCharset(), is(Charset.forName("UTF-8"))); checkStreamFromValue(fileValue, "text"); }
@Test public void testReadFileNameMimeTypeAndByteArray() throws IOException { InputStream is = this.getClass().getClassLoader().getResourceAsStream("org/camunda/bpm/engine/test/standalone/variables/simpleFile.txt"); byte[] data = new byte[is.available()]; DataInputStream dataInputStream = new DataInputStream(is); dataInputStream.readFully(data); dataInputStream.close(); MockValueFields valueFields = new MockValueFields(); String filename = "file.txt"; valueFields.setTextValue(filename); valueFields.setByteArrayValue(data); String mimeType = "text/plain"; valueFields.setTextValue2(mimeType + SEPARATOR); FileValue fileValue = serializer.readValue(valueFields, true); assertThat(fileValue.getFilename(), is(filename)); assertThat(fileValue.getMimeType(), is(mimeType)); checkStreamFromValue(fileValue, "text"); }
protected FileValue fileValueWithDecodedString(FileValue fileValue, String value) { return Variables.fileValue(fileValue.getFilename()) .file(Base64.decodeBase64(value)) .mimeType(fileValue.getMimeType()) .encoding(fileValue.getEncoding()) .create(); }
@Test public void testReadFullValue() throws IOException { InputStream is = this.getClass().getClassLoader().getResourceAsStream("org/camunda/bpm/engine/test/standalone/variables/simpleFile.txt"); byte[] data = new byte[is.available()]; DataInputStream dataInputStream = new DataInputStream(is); dataInputStream.readFully(data); dataInputStream.close(); MockValueFields valueFields = new MockValueFields(); String filename = "file.txt"; valueFields.setTextValue(filename); valueFields.setByteArrayValue(data); String mimeType = "text/plain"; String encoding = "UTF-16"; valueFields.setTextValue2(mimeType + SEPARATOR + encoding); FileValue fileValue = serializer.readValue(valueFields, true); assertThat(fileValue.getFilename(), is(filename)); assertThat(fileValue.getMimeType(), is(mimeType)); assertThat(fileValue.getEncoding(), is("UTF-16")); assertThat(fileValue.getEncodingAsCharset(), is(Charset.forName("UTF-16"))); checkStreamFromValue(fileValue, "text"); }
@Test public void testPostSingleFileVariableOnlyFilename() throws Exception { String variableKey = "aVariableKey"; String filename = "test.txt"; given() .pathParam("id", EXAMPLE_TASK_ID).pathParam("varId", variableKey) .multiPart("data", filename, new byte[0]) .multiPart("valueType", "File", "text/plain") .header("accept", MediaType.APPLICATION_JSON) .expect() .statusCode(Status.NO_CONTENT.getStatusCode()) .when() .post(SINGLE_TASK_SINGLE_BINARY_VARIABLE_URL); ArgumentCaptor<FileValue> captor = ArgumentCaptor.forClass(FileValue.class); verify(taskServiceMock).setVariable(eq(MockProvider.EXAMPLE_TASK_ID), eq(variableKey), captor.capture()); FileValue captured = captor.getValue(); assertThat(captured.getEncoding(), is(nullValue())); assertThat(captured.getFilename(), is(filename)); assertThat(captured.getMimeType(), is(MediaType.APPLICATION_OCTET_STREAM)); assertThat(captured.getValue().available(), is(0)); }
@Test public void testPostSingleFileVariableOnlyFilename() throws Exception { String variableKey = "aVariableKey"; String filename = "test.txt"; given() .pathParam("id", EXAMPLE_TASK_ID).pathParam("varId", variableKey) .multiPart("data", filename, new byte[0]) .multiPart("valueType", "File", "text/plain") .header("accept", MediaType.APPLICATION_JSON) .expect() .statusCode(Status.NO_CONTENT.getStatusCode()) .when() .post(SINGLE_PROCESS_INSTANCE_BINARY_VARIABLE_URL); ArgumentCaptor<FileValue> captor = ArgumentCaptor.forClass(FileValue.class); verify(runtimeServiceMock).setVariable(eq(MockProvider.EXAMPLE_TASK_ID), eq(variableKey), captor.capture()); FileValue captured = captor.getValue(); assertThat(captured.getEncoding(), is(nullValue())); assertThat(captured.getFilename(), is(filename)); assertThat(captured.getMimeType(), is(MediaType.APPLICATION_OCTET_STREAM)); assertThat(captured.getValue().available(), is(0)); }
@Test public void testPostSingleLocalFileVariableWithMimeType() { byte[] value = "some text".getBytes(); String variableKey = "aVariableKey"; String filename = "test.txt"; String mimetype = MediaType.TEXT_PLAIN; given() .pathParam("id", MockProvider.EXAMPLE_EXECUTION_ID).pathParam("varId", variableKey) .multiPart("data", filename, value, mimetype) .multiPart("valueType", "File", "text/plain") .header("accept", MediaType.APPLICATION_JSON) .expect() .statusCode(Status.NO_CONTENT.getStatusCode()) .when() .post(SINGLE_EXECUTION_LOCAL_BINARY_VARIABLE_URL); ArgumentCaptor<FileValue> captor = ArgumentCaptor.forClass(FileValue.class); verify(runtimeServiceMock).setVariableLocal(eq(MockProvider.EXAMPLE_EXECUTION_ID), eq(variableKey), captor.capture()); FileValue captured = captor.getValue(); assertThat(captured.getEncoding(), is(nullValue())); assertThat(captured.getFilename(), is(filename)); assertThat(captured.getMimeType(), is(mimetype)); assertThat(IoUtil.readInputStream(captured.getValue(), null), is(value)); }
@Test public void testPostSingleFileVariableWithMimeType() throws Exception { byte[] value = "some text".getBytes(); String variableKey = "aVariableKey"; String filename = "test.txt"; String mimetype = MediaType.TEXT_PLAIN; given() .pathParam("id", EXAMPLE_TASK_ID).pathParam("varId", variableKey) .multiPart("data", filename, value, mimetype) .multiPart("valueType", "File", "text/plain") .header("accept", MediaType.APPLICATION_JSON) .expect() .statusCode(Status.NO_CONTENT.getStatusCode()) .when() .post(SINGLE_PROCESS_INSTANCE_BINARY_VARIABLE_URL); ArgumentCaptor<FileValue> captor = ArgumentCaptor.forClass(FileValue.class); verify(runtimeServiceMock).setVariable(eq(MockProvider.EXAMPLE_TASK_ID), eq(variableKey), captor.capture()); FileValue captured = captor.getValue(); assertThat(captured.getEncoding(), is(nullValue())); assertThat(captured.getFilename(), is(filename)); assertThat(captured.getMimeType(), is(mimetype)); assertThat(IoUtil.readInputStream(captured.getValue(), null), is(value)); }
@Test public void testPostSingleFileVariableWithMimeType() throws Exception { byte[] value = "some text".getBytes(); String variableKey = "aVariableKey"; String filename = "test.txt"; String mimetype = MediaType.TEXT_PLAIN; given() .pathParam("id", MockProvider.EXAMPLE_CASE_INSTANCE_ID).pathParam("varId", variableKey) .multiPart("data", filename, value, mimetype) .multiPart("valueType", "File", "text/plain") .header("accept", MediaType.APPLICATION_JSON) .expect() .statusCode(Status.NO_CONTENT.getStatusCode()) .when() .post(SINGLE_CASE_INSTANCE_BINARY_VARIABLE_URL); ArgumentCaptor<FileValue> captor = ArgumentCaptor.forClass(FileValue.class); verify(caseServiceMock).withCaseExecution(MockProvider.EXAMPLE_CASE_INSTANCE_ID); verify(caseExecutionCommandBuilderMock).setVariable(eq(variableKey), captor.capture()); FileValue captured = captor.getValue(); assertThat(captured.getEncoding(), is(nullValue())); assertThat(captured.getFilename(), is(filename)); assertThat(captured.getMimeType(), is(mimetype)); assertThat(IoUtil.readInputStream(captured.getValue(), null), is(value)); }
@Test public void testPostSingleLocalFileVariableWithMimeType() throws Exception { byte[] value = "some text".getBytes(); String variableKey = "aVariableKey"; String filename = "test.txt"; String mimetype = MediaType.TEXT_PLAIN; given() .pathParam("id", MockProvider.EXAMPLE_CASE_EXECUTION_ID).pathParam("varId", variableKey) .multiPart("data", filename, value, mimetype) .multiPart("valueType", "File", "text/plain") .header("accept", MediaType.APPLICATION_JSON) .expect() .statusCode(Status.NO_CONTENT.getStatusCode()) .when() .post(SINGLE_CASE_EXECUTION_LOCAL_BINARY_VARIABLE_URL); ArgumentCaptor<FileValue> captor = ArgumentCaptor.forClass(FileValue.class); verify(caseServiceMock).withCaseExecution(MockProvider.EXAMPLE_CASE_EXECUTION_ID); verify(caseExecutionCommandBuilderMock).setVariableLocal(eq(variableKey), captor.capture()); FileValue captured = captor.getValue(); assertThat(captured.getEncoding(), is(nullValue())); assertThat(captured.getFilename(), is(filename)); assertThat(captured.getMimeType(), is(mimetype)); assertThat(IoUtil.readInputStream(captured.getValue(), null), is(value)); }
@Test public void testPostSingleLocalFileVariableWithEncodingAndMimeType() { byte[] value = "some text".getBytes(); String variableKey = "aVariableKey"; String encoding = "utf-8"; String filename = "test.txt"; String mimetype = MediaType.TEXT_PLAIN; given() .pathParam("id", MockProvider.EXAMPLE_EXECUTION_ID).pathParam("varId", variableKey) .multiPart("data", filename, value, mimetype + "; encoding="+encoding) .multiPart("valueType", "File", "text/plain") .header("accept", MediaType.APPLICATION_JSON) .expect() .statusCode(Status.NO_CONTENT.getStatusCode()) .when() .post(SINGLE_EXECUTION_LOCAL_BINARY_VARIABLE_URL); ArgumentCaptor<FileValue> captor = ArgumentCaptor.forClass(FileValue.class); verify(runtimeServiceMock).setVariableLocal(eq(MockProvider.EXAMPLE_EXECUTION_ID), eq(variableKey), captor.capture()); FileValue captured = captor.getValue(); assertThat(captured.getEncoding(), is(encoding)); assertThat(captured.getFilename(), is(filename)); assertThat(captured.getMimeType(), is(mimetype)); assertThat(IoUtil.readInputStream(captured.getValue(), null), is(value)); }
@Override public void writeValue(FileValue value, ValueFields valueFields) { byte[] data = ((FileValueImpl) value).getByteArray(); valueFields.setByteArrayValue(data); valueFields.setTextValue(value.getFilename()); if (value.getMimeType() == null && value.getEncoding() != null) { valueFields.setTextValue2(MIMETYPE_ENCODING_SEPARATOR + value.getEncoding()); } else if (value.getMimeType() != null && value.getEncoding() == null) { valueFields.setTextValue2(value.getMimeType() + MIMETYPE_ENCODING_SEPARATOR); } else if (value.getMimeType() != null && value.getEncoding() != null) { valueFields.setTextValue2(value.getMimeType() + MIMETYPE_ENCODING_SEPARATOR + value.getEncoding()); } }
@Override public void writeValue(FileValue value, ValueFields valueFields) { byte[] data = ((FileValueImpl) value).getByteArray(); valueFields.setByteArrayValue(data); valueFields.setTextValue(value.getFilename()); if (value.getMimeType() == null && value.getEncoding() != null) { valueFields.setTextValue2(MIMETYPE_ENCODING_SEPARATOR + value.getEncoding()); } else if (value.getMimeType() != null && value.getEncoding() == null) { valueFields.setTextValue2(value.getMimeType() + MIMETYPE_ENCODING_SEPARATOR); } else if (value.getMimeType() != null && value.getEncoding() != null) { valueFields.setTextValue2(value.getMimeType() + MIMETYPE_ENCODING_SEPARATOR + value.getEncoding()); } }
@Test public void testSerializeFileVariable() { BpmnModelInstance modelInstance = Bpmn.createExecutableProcess("process").startEvent().userTask().endEvent().done(); org.camunda.bpm.engine.repository.Deployment deployment = repositoryService.createDeployment().addModelInstance("process.bpmn", modelInstance).deploy(); VariableMap variables = Variables.createVariables(); String filename = "test.txt"; String type = "text/plain"; FileValue fileValue = Variables.fileValue(filename).file("ABC".getBytes()).encoding("UTF-8").mimeType(type).create(); variables.put("file", fileValue); runtimeService.startProcessInstanceByKey("process", variables); Task task = taskService.createTaskQuery().singleResult(); VariableInstance result = runtimeService.createVariableInstanceQuery().processInstanceIdIn(task.getProcessInstanceId()).singleResult(); FileValue value = (FileValue) result.getTypedValue(); assertThat(value.getFilename(), is(filename)); assertThat(value.getMimeType(), is(type)); assertThat(value.getEncoding(), is("UTF-8")); assertThat(value.getEncodingAsCharset(), is(Charset.forName("UTF-8"))); Scanner scanner = new Scanner(value.getValue()); assertThat(scanner.nextLine(), is("ABC")); // clean up repositoryService.deleteDeployment(deployment.getId(), true); }