A base writer class. @author Artem Labazin
| 27 | * @author Artem Labazin |
| 28 | */ |
| 29 | public abstract class AbstractWriter implements Writer { |
| 30 | |
| 31 | @Override |
| 32 | public void write(Output output, String boundary, String key, Object value) |
| 33 | throws EncodeException { |
| 34 | output.write("--").write(boundary).write(CRLF); |
| 35 | write(output, key, value); |
| 36 | output.write(CRLF); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Writes data for it's children. |
| 41 | * |
| 42 | * @param output output writer. |
| 43 | * @param key name for piece of data. |
| 44 | * @param value piece of data. |
| 45 | * @throws EncodeException in case of write errors |
| 46 | */ |
| 47 | @SuppressWarnings({ |
| 48 | "PMD.UncommentedEmptyMethodBody", |
| 49 | "PMD.EmptyMethodInAbstractClassShouldBeAbstract" |
| 50 | }) |
| 51 | protected void write(Output output, String key, Object value) throws EncodeException {} |
| 52 | |
| 53 | /** |
| 54 | * Writes file's metadata. |
| 55 | * |
| 56 | * @param output output writer. |
| 57 | * @param name name for piece of data. |
| 58 | * @param fileName file name. |
| 59 | * @param contentType type of file content. May be the {@code null}, in that case it will be |
| 60 | * determined by file name. |
| 61 | */ |
| 62 | protected void writeFileMetadata( |
| 63 | Output output, String name, String fileName, String contentType) { |
| 64 | val contentDespositionBuilder = |
| 65 | new StringBuilder() |
| 66 | .append("Content-Disposition: form-data; name=\"") |
| 67 | .append(name) |
| 68 | .append("\""); |
| 69 | if (fileName != null) { |
| 70 | contentDespositionBuilder.append("; ").append("filename=\"").append(fileName).append("\""); |
| 71 | } |
| 72 | |
| 73 | String fileContentType = contentType; |
| 74 | if (fileContentType == null) { |
| 75 | if (fileName != null) { |
| 76 | fileContentType = URLConnection.guessContentTypeFromName(fileName); |
| 77 | } |
| 78 | if (fileContentType == null) { |
| 79 | fileContentType = "application/octet-stream"; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | val string = |
| 84 | new StringBuilder() |
| 85 | .append(contentDespositionBuilder.toString()) |
| 86 | .append(CRLF) |
nothing calls this directly
no outgoing calls
no test coverage detected