/** * 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; }
@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 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())); }
@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 @Deployment(resources = ONE_TASK_PROCESS) public void testSerializeNullMimeTypeAndNullEncoding() { ProcessInstance pi = runtimeService.startProcessInstanceByKey("oneTaskProcess", Variables.createVariables().putValue("fileVar", Variables.fileValue("test.txt").file("ABC".getBytes()).create())); FileValue fileVar = runtimeService.getVariableTyped(pi.getId(), "fileVar"); assertNull(fileVar.getMimeType()); assertNull(fileVar.getEncoding()); }
@SuppressWarnings({ "unchecked" }) @Test public void testSubmitTaskFormWithFileValue() { String variableKey = "aVariable"; String filename = "test.txt"; Map<String, Object> variables = VariablesBuilder.create().variable(variableKey, Base64.encodeBase64String("someBytes".getBytes()), "File") .getVariables(); ((Map<String, Object>)variables.get(variableKey)).put("valueInfo", Collections.<String, Object>singletonMap("filename", filename)); Map<String, Object> json = new HashMap<String, Object>(); json.put("variables", variables); given().pathParam("id", EXAMPLE_TASK_ID) .header("accept", MediaType.APPLICATION_JSON) .contentType(POST_JSON_CONTENT_TYPE).body(json) .then().expect() .statusCode(Status.NO_CONTENT.getStatusCode()) .when().post(SUBMIT_FORM_URL); ArgumentCaptor<VariableMap> captor = ArgumentCaptor.forClass(VariableMap.class); verify(formServiceMock).submitTaskForm(eq(EXAMPLE_TASK_ID), captor.capture()); VariableMap map = captor.getValue(); FileValue fileValue = (FileValue) map.getValueTyped(variableKey); assertThat(fileValue, is(notNullValue())); assertThat(fileValue.getFilename(), is(filename)); assertThat(IoUtil.readInputStream(fileValue.getValue(), null), is("someBytes".getBytes())); }
@Test @Deployment(resources = ONE_TASK_PROCESS) public void testSerializeNullMimeType() { ProcessInstance pi = runtimeService.startProcessInstanceByKey("oneTaskProcess", Variables.createVariables().putValue("fileVar", Variables.fileValue("test.txt").file("ABC".getBytes()).encoding("UTF-8").create())); FileValue fileVar = runtimeService.getVariableTyped(pi.getId(), "fileVar"); assertNull(fileVar.getMimeType()); }
@Test @Deployment(resources = ONE_TASK_PROCESS) public void testSerializeNullEncoding() { ProcessInstance pi = runtimeService.startProcessInstanceByKey("oneTaskProcess", Variables.createVariables().putValue("fileVar", Variables.fileValue("test.txt").mimeType("some mimetype").file("ABC".getBytes()).create())); FileValue fileVar = runtimeService.getVariableTyped(pi.getId(), "fileVar"); assertNull(fileVar.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 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"); }
@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())); }
@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 @Deployment(resources = ONE_TASK_PROCESS) public void testSerializeNullMimeTypeAndNullEncoding() { ProcessInstance pi = runtimeService.startProcessInstanceByKey("oneTaskProcess", Variables.createVariables().putValue("fileVar", Variables.fileValue("test.txt").file("ABC".getBytes()).create())); FileValue fileVar = runtimeService.getVariableTyped(pi.getId(), "fileVar"); assertNull(fileVar.getMimeType()); assertNull(fileVar.getEncoding()); }
@Test @Deployment(resources = ONE_TASK_PROCESS) public void testSerializeNullValue() { ProcessInstance pi = runtimeService.startProcessInstanceByKey("oneTaskProcess", Variables.createVariables().putValue("fileVar", Variables.fileValue("test.txt").create())); FileValue fileVar = runtimeService.getVariableTyped(pi.getId(), "fileVar"); assertNull(fileVar.getMimeType()); }
@Test @Deployment(resources = ONE_TASK_PROCESS) public void testSerializeNullEncoding() { ProcessInstance pi = runtimeService.startProcessInstanceByKey("oneTaskProcess", Variables.createVariables().putValue("fileVar", Variables.fileValue("test.txt").mimeType("some mimetype").file("ABC".getBytes()).create())); FileValue fileVar = runtimeService.getVariableTyped(pi.getId(), "fileVar"); assertNull(fileVar.getEncoding()); }
/** * 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(); }
protected FileValue fileValueWithDecodedString(FileValue fileValue, String value) { return Variables.fileValue(fileValue.getFilename()) .file(Base64.decodeBase64(value)) .mimeType(fileValue.getMimeType()) .encoding(fileValue.getEncoding()) .create(); }
@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; }