-
Notifications
You must be signed in to change notification settings - Fork 7
/
HelloTransformer.java
82 lines (72 loc) · 3.03 KB
/
HelloTransformer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* #%L
* Alfresco Transform Core
* %%
* Copyright (C) 2022 - 2022 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* -
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
* -
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* -
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* -
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.transform;
import org.alfresco.transform.base.CustomTransformer;
import org.alfresco.transform.base.TransformManager;
import org.alfresco.transform.exceptions.TransformException;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
@Component
public class HelloTransformer implements CustomTransformer
{
private static final String HTML_TEMPLATE = "<!DOCTYPE html><html><head><meta charset=\"utf-8\">" +
"<title>Hello World</title></head><body><h1 style=\"color:blue\">T-Engine Example</h1><p>%s</p></body></html>";
private static final Map<String, String> HW_DICTIONARY = new HashMap<>();
static
{
HW_DICTIONARY.put("english", "Hello World! Hello %s!");
HW_DICTIONARY.put("spanish", "¡Hola Mundo! ¡Hola %s!");
HW_DICTIONARY.put("german", "Hallo Welt! Hallo %s! ");
}
@Override
public String getTransformerName()
{
return "helloWorld";
}
@Override
public void transform(String sourceMimetype, InputStream inputStream, String targetMimetype,
OutputStream outputStream, Map<String, String> transformOptions, TransformManager transformManager)
throws Exception
{
String language = transformOptions.get("language");
String nameFromFile = new String(inputStream.readAllBytes());
// Deliberately not doing the NPE check on language, so it is possible to see how that is logged
String greeting = HW_DICTIONARY.get(language.toLowerCase());
if (greeting == null)
{
throw new IllegalArgumentException("We don't have a greeting for input language '" + language + "'." );
}
String bodyContent = String.format(greeting, nameFromFile);
String html = String.format(HTML_TEMPLATE, bodyContent);
byte[] bytes = html.getBytes();
outputStream.write(bytes, 0, bytes.length);
}
}