1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.macro.chart; |
21 |
|
|
22 |
|
import java.io.File; |
23 |
|
import java.io.FileOutputStream; |
24 |
|
import java.io.IOException; |
25 |
|
import java.net.URLEncoder; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Named; |
29 |
|
import javax.inject.Singleton; |
30 |
|
|
31 |
|
import org.apache.commons.io.IOUtils; |
32 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
33 |
|
import org.xwiki.component.annotation.Component; |
34 |
|
import org.xwiki.environment.Environment; |
35 |
|
import org.xwiki.model.EntityType; |
36 |
|
import org.xwiki.model.ModelContext; |
37 |
|
import org.xwiki.model.reference.DocumentReference; |
38 |
|
import org.xwiki.model.reference.EntityReference; |
39 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
@version |
45 |
|
@since |
46 |
|
|
47 |
|
@Component |
48 |
|
@Named("tmp") |
49 |
|
@Singleton |
|
|
| 86.2% |
Uncovered Elements: 4 (29) |
Complexity: 7 |
Complexity Density: 0.3 |
|
50 |
|
public class TemporaryChartImageWriter implements ChartImageWriter |
51 |
|
{ |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
private static final String DEFAULT_ENCODING = "UTF-8"; |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
private static final String MODULE_NAME = "chart"; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
private static final String SPACE = "space"; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
private static final String PAGE = "page"; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@Inject |
78 |
|
private Environment environment; |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
@Inject |
84 |
|
private ModelContext modelContext; |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
@Inject |
90 |
|
private DocumentAccessBridge documentAccessBridge; |
91 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
|
92 |
15 |
@Override... |
93 |
|
public void writeImage(ImageId imageId, byte[] imageData) throws MacroExecutionException |
94 |
|
{ |
95 |
15 |
File imageFile = getStorageLocation(imageId); |
96 |
|
|
97 |
15 |
FileOutputStream fos = null; |
98 |
15 |
try { |
99 |
15 |
fos = new FileOutputStream(imageFile); |
100 |
15 |
fos.write(imageData); |
101 |
15 |
fos.close(); |
102 |
|
} catch (IOException e) { |
103 |
0 |
throw new MacroExecutionException("Failed to write the generated chart image", e); |
104 |
|
} finally { |
105 |
15 |
IOUtils.closeQuietly(fos); |
106 |
|
} |
107 |
|
} |
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
@param |
113 |
|
@return |
114 |
|
@throws |
115 |
|
|
|
|
| 87.5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0.25 |
|
116 |
16 |
protected File getStorageLocation(ImageId imageId) throws MacroExecutionException... |
117 |
|
{ |
118 |
16 |
File directory; |
119 |
16 |
try { |
120 |
16 |
String currentWiki = URLEncoder.encode(getCurrentWiki(), DEFAULT_ENCODING); |
121 |
|
|
122 |
|
|
123 |
|
|
124 |
16 |
directory = new File(this.environment.getTemporaryDirectory(), |
125 |
|
String.format("temp/%s/%s/%s/%s", MODULE_NAME, currentWiki, SPACE, PAGE)); |
126 |
16 |
directory.mkdirs(); |
127 |
|
} catch (Exception e) { |
128 |
|
|
129 |
0 |
throw new MacroExecutionException("Failed to compute chart image location", e); |
130 |
|
} |
131 |
16 |
File locationFile = new File(directory, String.format("%s.png", imageId.getId())); |
132 |
16 |
return locationFile; |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
@return |
137 |
|
@throws |
138 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
139 |
32 |
private String getCurrentWiki() throws MacroExecutionException... |
140 |
|
{ |
141 |
32 |
EntityReference reference = this.modelContext.getCurrentEntityReference(); |
142 |
32 |
if (reference == null) { |
143 |
|
|
144 |
0 |
throw new MacroExecutionException("The current wiki couldn't be computed"); |
145 |
|
} |
146 |
32 |
return reference.extractReference(EntityType.WIKI).getName(); |
147 |
|
} |
148 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
149 |
16 |
@Override... |
150 |
|
public String getURL(ImageId imageId) throws MacroExecutionException |
151 |
|
{ |
152 |
16 |
DocumentReference reference = new DocumentReference(getCurrentWiki(), SPACE, PAGE); |
153 |
16 |
String prefix = this.documentAccessBridge.getDocumentURL(reference, "temp", null, null); |
154 |
16 |
return String.format("%s/%s/%s.png", prefix, MODULE_NAME, imageId.getId()); |
155 |
|
} |
156 |
|
} |