Skip to content

Commit 5516265

Browse files
committed
fix OLAP instantiation
1 parent 8496b07 commit 5516265

3 files changed

Lines changed: 44 additions & 3 deletions

File tree

knowageutils/src/main/java/it/eng/spagobi/utilities/engines/EngineStartServletIOManager.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ public JSONObject getTemplateAsJSONObject() {
218218
templateJSON = template != null ? new JSONObject(template) : null;
219219
} catch (Throwable t) {
220220
logger.error("Impossible to decode template's content\n" + t);
221-
throw new SpagoBIRuntimeException("Impossible to decode template's content [" + template.getFileName() + "]", t);
221+
throw new SpagoBIRuntimeException("Impossible to decode template's content [" + getCurrentTemplateName() + "]", t);
222222

223223
}
224224

@@ -232,7 +232,7 @@ public SourceBean getTemplateAsSourceBean() {
232232
templateSB = TemplateSourceBeanParser.parse(template);
233233
} catch (SourceBeanException e) {
234234
logger.error("Impossible to decode template's content\n" + e);
235-
throw new SpagoBIRuntimeException("Impossible to decode template's content [" + template.getFileName() + "]", e);
235+
throw new SpagoBIRuntimeException("Impossible to decode template's content [" + getCurrentTemplateName() + "]", e);
236236

237237
}
238238

@@ -288,6 +288,16 @@ public String getTemplateName() {
288288
return templateName;
289289
}
290290

291+
private String getCurrentTemplateName() {
292+
if (templateName != null) {
293+
return templateName;
294+
}
295+
if (template != null) {
296+
return template.getFileName();
297+
}
298+
return "unknown";
299+
}
300+
291301
public IDataSource getDataSource() {
292302
if (dataSource == null) {
293303
String connectionName = getParameterAsString("connectionName");

knowageutils/src/main/java/it/eng/spagobi/utilities/engines/TemplateSourceBeanParser.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,38 @@
2323

2424
import it.eng.spago.base.SourceBean;
2525
import it.eng.spago.base.SourceBeanException;
26+
import it.eng.spago.util.XMLUtil;
2627

2728
public final class TemplateSourceBeanParser {
2829

2930
private TemplateSourceBeanParser() {
3031
}
3132

3233
public static SourceBean parse(String templateContent) throws SourceBeanException {
34+
String normalizedTemplateContent = normalizeTemplateContent(templateContent);
35+
if (normalizedTemplateContent == null) {
36+
return null;
37+
}
38+
return SourceBean.fromXMLStream(new InputSource(new StringReader(normalizedTemplateContent)));
39+
}
40+
41+
private static String normalizeTemplateContent(String templateContent) throws SourceBeanException {
3342
String sanitizedTemplateContent = sanitizeTemplateContent(templateContent);
3443
if (sanitizedTemplateContent == null) {
3544
return null;
3645
}
3746
if (sanitizedTemplateContent.isEmpty()) {
3847
throw new SourceBeanException("xmlSourceBean non valido");
3948
}
40-
return SourceBean.fromXMLStream(new InputSource(new StringReader(sanitizedTemplateContent)));
49+
if (!sanitizedTemplateContent.startsWith("<")) {
50+
String wrappedTemplateContent = getXmlHeader() + "\n<XMLSOURCEBEAN>\n" + sanitizedTemplateContent + "\n</XMLSOURCEBEAN>";
51+
SourceBean wrappedTemplateSourceBean = SourceBean.fromXMLStream(new InputSource(new StringReader(wrappedTemplateContent)));
52+
sanitizedTemplateContent = wrappedTemplateSourceBean.getCharacters();
53+
}
54+
if (!sanitizedTemplateContent.startsWith(XMLUtil.XML_HEADER_PREFIX)) {
55+
sanitizedTemplateContent = getXmlHeader() + "\n" + sanitizedTemplateContent;
56+
}
57+
return sanitizedTemplateContent;
4158
}
4259

4360
private static String sanitizeTemplateContent(String templateContent) {
@@ -51,4 +68,9 @@ private static String removeUtf8Bom(String templateContent) {
5168
}
5269
return templateContent;
5370
}
71+
72+
private static String getXmlHeader() {
73+
return XMLUtil.XML_HEADER_PREFIX + " version=\"" + XMLUtil.XML_HEADER_DEFAULT_VERSION + "\" "
74+
+ "encoding=\"" + XMLUtil.XML_HEADER_DEFAULT_ENCODING + "\"?>";
75+
}
5476
}

knowageutils/src/test/java/it/eng/spagobi/utilities/engines/rest/AbstractEngineRestServiceTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ public void testGetTemplateAsSourceBeanParsesXmlWithoutDeclaration() {
3636
Assert.assertNotNull(template.getAttribute("CUBE"));
3737
}
3838

39+
@Test
40+
public void testGetTemplateAsSourceBeanParsesXmlWithDeclaration() {
41+
SourceBean template = new TestEngineRestService("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><olap><cube reference=\"FoodMartMySQL\"/></olap>")
42+
.getTemplateAsSourceBean();
43+
44+
Assert.assertEquals("OLAP", template.getName());
45+
Assert.assertNotNull(template.getAttribute("CUBE"));
46+
}
47+
3948
@Test
4049
public void testGetTemplateAsSourceBeanStripsUtf8Bom() {
4150
SourceBean template = new TestEngineRestService("\uFEFF<olap><cube reference=\"FoodMartMySQL\"/></olap>")

0 commit comments

Comments
 (0)