Drop deprecated dependencies on Log4j, JRuby, JExcel, Burlap, Commons Pool/DBCP

This commit also removes outdated support classes for Oracle, GlassFish, JBoss.

Issue: SPR-14429
This commit is contained in:
Juergen Hoeller
2016-07-05 15:46:53 +02:00
parent fb5a096ca2
commit 0fc0ce78ae
46 changed files with 24 additions and 4916 deletions

View File

@@ -1,224 +0,0 @@
/*
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.view.document;
import java.util.Locale;
import java.util.Map;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.LocalizedResourceHelper;
import org.springframework.web.servlet.support.RequestContextUtils;
import org.springframework.web.servlet.view.AbstractView;
/**
* Convenient superclass for Excel document views.
* Compatible with Apache POI 3.5 and higher, as of Spring 4.0.
*
* <p>Properties:
* <ul>
* <li>url (optional): The url of an existing Excel document to pick as a starting point.
* It is done without localization part nor the ".xls" extension.
* </ul>
*
* <p>The file will be searched with locations in the following order:
* <ul>
* <li>[url]_[language]_[country].xls
* <li>[url]_[language].xls
* <li>[url].xls
* </ul>
*
* <p>For working with the workbook in the subclass, see
* <a href="http://poi.apache.org">Apache's POI site</a>
*
* <p>As an example, you can try this snippet:
*
* <pre class="code">
* protected void buildExcelDocument(
* Map&lt;String, Object&gt; model, HSSFWorkbook workbook,
* HttpServletRequest request, HttpServletResponse response) {
*
* // Go to the first sheet.
* // getSheetAt: only if workbook is created from an existing document
* // HSSFSheet sheet = workbook.getSheetAt(0);
* HSSFSheet sheet = workbook.createSheet("Spring");
* sheet.setDefaultColumnWidth(12);
*
* // Write a text at A1.
* HSSFCell cell = getCell(sheet, 0, 0);
* setText(cell, "Spring POI test");
*
* // Write the current date at A2.
* HSSFCellStyle dateStyle = workbook.createCellStyle();
* dateStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy"));
* cell = getCell(sheet, 1, 0);
* cell.setCellValue(new Date());
* cell.setCellStyle(dateStyle);
*
* // Write a number at A3
* getCell(sheet, 2, 0).setCellValue(458);
*
* // Write a range of numbers.
* HSSFRow sheetRow = sheet.createRow(3);
* for (short i = 0; i < 10; i++) {
* sheetRow.createCell(i).setCellValue(i * 10);
* }
* }</pre>
*
* This class is similar to the AbstractPdfView class in usage style.
*
* @author Jean-Pierre Pawlak
* @author Juergen Hoeller
* @see AbstractPdfView
* @deprecated as of Spring 4.2, in favor of {@link AbstractXlsView} and its
* {@link AbstractXlsxView} and {@link AbstractXlsxStreamingView} variants
*/
@Deprecated
public abstract class AbstractExcelView extends AbstractView {
/** The content type for an Excel response */
private static final String CONTENT_TYPE = "application/vnd.ms-excel";
/** The extension to look for existing templates */
private static final String EXTENSION = ".xls";
private String url;
/**
* Default Constructor.
* Sets the content type of the view to "application/vnd.ms-excel".
*/
public AbstractExcelView() {
setContentType(CONTENT_TYPE);
}
/**
* Set the URL of the Excel workbook source, without localization part nor extension.
*/
public void setUrl(String url) {
this.url = url;
}
@Override
protected boolean generatesDownloadContent() {
return true;
}
/**
* Renders the Excel view, given the specified model.
*/
@Override
protected final void renderMergedOutputModel(
Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
HSSFWorkbook workbook;
if (this.url != null) {
workbook = getTemplateSource(this.url, request);
}
else {
workbook = new HSSFWorkbook();
logger.debug("Created Excel Workbook from scratch");
}
buildExcelDocument(model, workbook, request, response);
// Set the content type.
response.setContentType(getContentType());
// Should we set the content length here?
// response.setContentLength(workbook.getBytes().length);
// Flush byte array to servlet output stream.
ServletOutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
}
/**
* Creates the workbook from an existing XLS document.
* @param url the URL of the Excel template without localization part nor extension
* @param request current HTTP request
* @return the HSSFWorkbook
* @throws Exception in case of failure
*/
protected HSSFWorkbook getTemplateSource(String url, HttpServletRequest request) throws Exception {
LocalizedResourceHelper helper = new LocalizedResourceHelper(getApplicationContext());
Locale userLocale = RequestContextUtils.getLocale(request);
Resource inputFile = helper.findLocalizedResource(url, EXTENSION, userLocale);
// Create the Excel document from the source.
if (logger.isDebugEnabled()) {
logger.debug("Loading Excel workbook from " + inputFile);
}
return new HSSFWorkbook(inputFile.getInputStream());
}
/**
* Subclasses must implement this method to create an Excel HSSFWorkbook document,
* given the model.
* @param model the model Map
* @param workbook the Excel workbook to complete
* @param request in case we need locale etc. Shouldn't look at attributes.
* @param response in case we need to set cookies. Shouldn't write to it.
*/
protected abstract void buildExcelDocument(
Map<String, Object> model, HSSFWorkbook workbook, HttpServletRequest request, HttpServletResponse response)
throws Exception;
/**
* Convenient method to obtain the cell in the given sheet, row and column.
* <p>Creates the row and the cell if they still doesn't already exist.
* Thus, the column can be passed as an int, the method making the needed downcasts.
* @param sheet a sheet object. The first sheet is usually obtained by workbook.getSheetAt(0)
* @param row the row number
* @param col the column number
* @return the HSSFCell
*/
protected HSSFCell getCell(HSSFSheet sheet, int row, int col) {
HSSFRow sheetRow = sheet.getRow(row);
if (sheetRow == null) {
sheetRow = sheet.createRow(row);
}
HSSFCell cell = sheetRow.getCell(col);
if (cell == null) {
cell = sheetRow.createCell(col);
}
return cell;
}
/**
* Convenient method to set a String as text content in a cell.
* @param cell the cell in which the text must be put
* @param text the text to put in the cell
*/
protected void setText(HSSFCell cell, String text) {
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(text);
}
}

View File

@@ -1,181 +0,0 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.view.document;
import java.io.OutputStream;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;
import jxl.write.WritableWorkbook;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.LocalizedResourceHelper;
import org.springframework.web.servlet.support.RequestContextUtils;
import org.springframework.web.servlet.view.AbstractView;
/**
* Convenient superclass for Excel document views.
*
* <p>This class uses the <i>JExcelAPI</i> instead of <i>POI</i>.
* More information on <i>JExcelAPI</i> can be found on their
* <a href="http://www.andykhan.com/jexcelapi/" target="_blank">website</a>.
*
* <p>Properties:
* <ul>
* <li>url (optional): The url of an existing Excel document to pick as a
* starting point. It is done without localization part nor the .xls extension.
* </ul>
*
* <p>The file will be searched with locations in the following order:
* <ul>
* <li>[url]_[language]_[country].xls
* <li>[url]_[language].xls
* <li>[url].xls
* </ul>
*
* <p>For working with the workbook in the subclass, see <a
* href="http://www.andykhan.com/jexcelapi/">Java Excel API site</a>
*
* <p>As an example, you can try this snippet:
*
* <pre class="code">
* protected void buildExcelDocument(
* Map&lt;String, Object&gt; model, WritableWorkbook workbook,
* HttpServletRequest request, HttpServletResponse response) {
*
* if (workbook.getNumberOfSheets() == 0) {
* workbook.createSheet(&quot;Spring&quot;, 0);
* }
*
* WritableSheet sheet = workbook.getSheet(&quot;Spring&quot;);
* Label label = new Label(0, 0, &quot;This is a nice label&quot;);
* sheet.addCell(label);
* }</pre>
*
* The use of this view is close to the {@link AbstractExcelView} class,
* just using the JExcel API instead of the Apache POI API.
*
* @author Bram Smeets
* @author Alef Arendsen
* @author Juergen Hoeller
* @since 1.2.5
* @see AbstractExcelView
* @see AbstractPdfView
* @deprecated as of Spring 4.0, since JExcelAPI is an abandoned project
* (no release since 2009, with serious bugs remaining)
*/
@Deprecated
public abstract class AbstractJExcelView extends AbstractView {
/** The content type for an Excel response */
private static final String CONTENT_TYPE = "application/vnd.ms-excel";
/** The extension to look for existing templates */
private static final String EXTENSION = ".xls";
/** The url at which the template to use is located */
private String url;
/**
* Default Constructor.
* Sets the content type of the view to "application/vnd.ms-excel".
*/
public AbstractJExcelView() {
setContentType(CONTENT_TYPE);
}
/**
* Set the URL of the Excel workbook source, without localization part nor extension.
*/
public void setUrl(String url) {
this.url = url;
}
@Override
protected boolean generatesDownloadContent() {
return true;
}
/**
* Renders the Excel view, given the specified model.
*/
@Override
protected final void renderMergedOutputModel(
Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
// Set the content type and get the output stream.
response.setContentType(getContentType());
OutputStream out = response.getOutputStream();
WritableWorkbook workbook;
if (this.url != null) {
Workbook template = getTemplateSource(this.url, request);
workbook = Workbook.createWorkbook(out, template);
}
else {
logger.debug("Creating Excel Workbook from scratch");
workbook = Workbook.createWorkbook(out);
}
buildExcelDocument(model, workbook, request, response);
// Should we set the content length here?
// response.setContentLength(workbook.getBytes().length);
workbook.write();
out.flush();
workbook.close();
}
/**
* Create the workbook from an existing XLS document.
* @param url the URL of the Excel template without localization part nor extension
* @param request current HTTP request
* @return the template workbook
* @throws Exception in case of failure
*/
protected Workbook getTemplateSource(String url, HttpServletRequest request) throws Exception {
LocalizedResourceHelper helper = new LocalizedResourceHelper(getApplicationContext());
Locale userLocale = RequestContextUtils.getLocale(request);
Resource inputFile = helper.findLocalizedResource(url, EXTENSION, userLocale);
// Create the Excel document from the source.
if (logger.isDebugEnabled()) {
logger.debug("Loading Excel workbook from " + inputFile);
}
return Workbook.getWorkbook(inputFile.getInputStream());
}
/**
* Subclasses must implement this method to create an Excel Workbook
* document, given the model.
* @param model the model Map
* @param workbook the Excel workbook to complete
* @param request in case we need locale etc. Shouldn't look at attributes.
* @param response in case we need to set cookies. Shouldn't write to it.
* @throws Exception in case of failure
*/
protected abstract void buildExcelDocument(Map<String, Object> model, WritableWorkbook workbook,
HttpServletRequest request, HttpServletResponse response) throws Exception;
}

View File

@@ -101,7 +101,6 @@ import org.springframework.web.servlet.handler.ConversionServiceExposingIntercep
import org.springframework.web.servlet.handler.MappedInterceptor;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor;
import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;
import org.springframework.web.servlet.mvc.ParameterizableViewController;
@@ -143,8 +142,8 @@ import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
import org.springframework.web.util.UrlPathHelper;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
/**
@@ -307,7 +306,7 @@ public class MvcNamespaceTests {
@Test
public void testInterceptors() throws Exception {
loadBeanDefinitions("mvc-config-interceptors.xml", 21);
loadBeanDefinitions("mvc-config-interceptors.xml", 18);
RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
assertNotNull(mapping);
@@ -319,26 +318,23 @@ public class MvcNamespaceTests {
request.addParameter("theme", "green");
HandlerExecutionChain chain = mapping.getHandler(request);
assertEquals(5, chain.getInterceptors().length);
assertEquals(4, chain.getInterceptors().length);
assertTrue(chain.getInterceptors()[0] instanceof ConversionServiceExposingInterceptor);
assertTrue(chain.getInterceptors()[1] instanceof LocaleChangeInterceptor);
assertTrue(chain.getInterceptors()[2] instanceof WebRequestHandlerInterceptorAdapter);
assertTrue(chain.getInterceptors()[3] instanceof ThemeChangeInterceptor);
assertTrue(chain.getInterceptors()[4] instanceof UserRoleAuthorizationInterceptor);
assertTrue(chain.getInterceptors()[2] instanceof ThemeChangeInterceptor);
assertTrue(chain.getInterceptors()[3] instanceof UserRoleAuthorizationInterceptor);
request.setRequestURI("/admin/users");
chain = mapping.getHandler(request);
assertEquals(3, chain.getInterceptors().length);
assertEquals(2, chain.getInterceptors().length);
request.setRequestURI("/logged/accounts/12345");
chain = mapping.getHandler(request);
assertEquals(5, chain.getInterceptors().length);
assertTrue(chain.getInterceptors()[4] instanceof WebRequestHandlerInterceptorAdapter);
assertEquals(3, chain.getInterceptors().length);
request.setRequestURI("/foo/logged");
chain = mapping.getHandler(request);
assertEquals(5, chain.getInterceptors().length);
assertTrue(chain.getInterceptors()[4] instanceof WebRequestHandlerInterceptorAdapter);
assertEquals(3, chain.getInterceptors().length);
}
@Test
@@ -1017,6 +1013,11 @@ public class MvcNamespaceTests {
return null;
}
}
@Override
public String getVirtualServerName() {
return null;
}
}
public static class TestCallableProcessingInterceptor extends CallableProcessingInterceptorAdapter {

View File

@@ -1,349 +0,0 @@
/*
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.view.document;
import java.io.ByteArrayInputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.read.biff.WorkbookParser;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.mock.web.test.MockServletContext;
import org.springframework.util.ReflectionUtils;
import org.springframework.web.context.support.StaticWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.LocaleResolver;
import static org.junit.Assert.*;
/**
* Tests for the AbstractExcelView and the AbstractJExcelView classes.
*
* @author Alef Arendsen
* @author Bram Smeets
*/
@SuppressWarnings("deprecation")
public class ExcelViewTests {
private MockHttpServletRequest request;
private MockHttpServletResponse response;
private StaticWebApplicationContext webAppCtx;
@Before
public void setUp() {
MockServletContext servletCtx = new MockServletContext("org/springframework/web/servlet/view/document");
request = new MockHttpServletRequest(servletCtx);
response = new MockHttpServletResponse();
webAppCtx = new StaticWebApplicationContext();
webAppCtx.setServletContext(servletCtx);
}
@Test
public void testExcel() throws Exception {
AbstractExcelView excelView = new AbstractExcelView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook wb,
HttpServletRequest request, HttpServletResponse response) throws Exception {
HSSFSheet sheet = wb.createSheet("Test Sheet");
// test all possible permutation of row or column not existing
HSSFCell cell = getCell(sheet, 2, 4);
cell.setCellValue("Test Value");
cell = getCell(sheet, 2, 3);
setText(cell, "Test Value");
cell = getCell(sheet, 3, 4);
setText(cell, "Test Value");
cell = getCell(sheet, 2, 4);
setText(cell, "Test Value");
}
};
excelView.render(new HashMap<String, Object>(), request, response);
HSSFWorkbook wb = new HSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
assertEquals("Test Sheet", wb.getSheetName(0));
HSSFSheet sheet = wb.getSheet("Test Sheet");
HSSFRow row = sheet.getRow(2);
HSSFCell cell = row.getCell(4);
assertEquals("Test Value", cell.getStringCellValue());
}
@Test
public void testExcelWithTemplateNoLoc() throws Exception {
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
newDummyLocaleResolver("nl", "nl"));
AbstractExcelView excelView = new AbstractExcelView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook wb,
HttpServletRequest request, HttpServletResponse response) throws Exception {
HSSFSheet sheet = wb.getSheet("Sheet1");
// test all possible permutation of row or column not existing
HSSFCell cell = getCell(sheet, 2, 4);
cell.setCellValue("Test Value");
cell = getCell(sheet, 2, 3);
setText(cell, "Test Value");
cell = getCell(sheet, 3, 4);
setText(cell, "Test Value");
cell = getCell(sheet, 2, 4);
setText(cell, "Test Value");
}
};
excelView.setApplicationContext(webAppCtx);
excelView.setUrl("template");
excelView.render(new HashMap<String, Object>(), request, response);
HSSFWorkbook wb = new HSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
HSSFSheet sheet = wb.getSheet("Sheet1");
HSSFRow row = sheet.getRow(0);
HSSFCell cell = row.getCell(0);
assertEquals("Test Template", cell.getStringCellValue());
}
@Test
public void testExcelWithTemplateAndCountryAndLanguage() throws Exception {
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
newDummyLocaleResolver("en", "US"));
AbstractExcelView excelView = new AbstractExcelView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook wb,
HttpServletRequest request, HttpServletResponse response) throws Exception {
HSSFSheet sheet = wb.getSheet("Sheet1");
// test all possible permutation of row or column not existing
HSSFCell cell = getCell(sheet, 2, 4);
cell.setCellValue("Test Value");
cell = getCell(sheet, 2, 3);
setText(cell, "Test Value");
cell = getCell(sheet, 3, 4);
setText(cell, "Test Value");
cell = getCell(sheet, 2, 4);
setText(cell, "Test Value");
}
};
excelView.setApplicationContext(webAppCtx);
excelView.setUrl("template");
excelView.render(new HashMap<String, Object>(), request, response);
HSSFWorkbook wb = new HSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
HSSFSheet sheet = wb.getSheet("Sheet1");
HSSFRow row = sheet.getRow(0);
HSSFCell cell = row.getCell(0);
assertEquals("Test Template American English", cell.getStringCellValue());
}
@Test
public void testExcelWithTemplateAndLanguage() throws Exception {
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
newDummyLocaleResolver("de", ""));
AbstractExcelView excelView = new AbstractExcelView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, HSSFWorkbook wb,
HttpServletRequest request, HttpServletResponse response) throws Exception {
HSSFSheet sheet = wb.getSheet("Sheet1");
// test all possible permutation of row or column not existing
HSSFCell cell = getCell(sheet, 2, 4);
cell.setCellValue("Test Value");
cell = getCell(sheet, 2, 3);
setText(cell, "Test Value");
cell = getCell(sheet, 3, 4);
setText(cell, "Test Value");
cell = getCell(sheet, 2, 4);
setText(cell, "Test Value");
}
};
excelView.setApplicationContext(webAppCtx);
excelView.setUrl("template");
excelView.render(new HashMap<String, Object>(), request, response);
HSSFWorkbook wb = new HSSFWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
HSSFSheet sheet = wb.getSheet("Sheet1");
HSSFRow row = sheet.getRow(0);
HSSFCell cell = row.getCell(0);
assertEquals("Test Template auf Deutsch", cell.getStringCellValue());
}
@Test
public void testJExcel() throws Exception {
AbstractJExcelView excelView = new UnixSafeAbstractJExcelView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, WritableWorkbook wb,
HttpServletRequest request, HttpServletResponse response) throws Exception {
WritableSheet sheet = wb.createSheet("Test Sheet", 0);
// test all possible permutation of row or column not existing
sheet.addCell(new Label(2, 4, "Test Value"));
sheet.addCell(new Label(2, 3, "Test Value"));
sheet.addCell(new Label(3, 4, "Test Value"));
sheet.addCell(new Label(2, 4, "Test Value"));
}
};
excelView.render(new HashMap<String, Object>(), request, response);
Workbook wb = Workbook.getWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
assertEquals("Test Sheet", wb.getSheet(0).getName());
Sheet sheet = wb.getSheet("Test Sheet");
Cell cell = sheet.getCell(2, 4);
assertEquals("Test Value", cell.getContents());
}
@Test
public void testJExcelWithTemplateNoLoc() throws Exception {
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
newDummyLocaleResolver("nl", "nl"));
AbstractJExcelView excelView = new UnixSafeAbstractJExcelView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, WritableWorkbook wb,
HttpServletRequest request, HttpServletResponse response) throws Exception {
WritableSheet sheet = wb.getSheet("Sheet1");
// test all possible permutation of row or column not existing
sheet.addCell(new Label(2, 4, "Test Value"));
sheet.addCell(new Label(2, 3, "Test Value"));
sheet.addCell(new Label(3, 4, "Test Value"));
sheet.addCell(new Label(2, 4, "Test Value"));
}
};
excelView.setApplicationContext(webAppCtx);
excelView.setUrl("template");
excelView.render(new HashMap<String, Object>(), request, response);
Workbook wb = Workbook.getWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
Sheet sheet = wb.getSheet("Sheet1");
Cell cell = sheet.getCell(0, 0);
assertEquals("Test Template", cell.getContents());
}
@Test
public void testJExcelWithTemplateAndCountryAndLanguage() throws Exception {
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
newDummyLocaleResolver("en", "US"));
AbstractJExcelView excelView = new UnixSafeAbstractJExcelView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, WritableWorkbook wb,
HttpServletRequest request, HttpServletResponse response) throws Exception {
WritableSheet sheet = wb.getSheet("Sheet1");
// test all possible permutation of row or column not existing
sheet.addCell(new Label(2, 4, "Test Value"));
sheet.addCell(new Label(2, 3, "Test Value"));
sheet.addCell(new Label(3, 4, "Test Value"));
sheet.addCell(new Label(2, 4, "Test Value"));
}
};
excelView.setApplicationContext(webAppCtx);
excelView.setUrl("template");
excelView.render(new HashMap<String, Object>(), request, response);
Workbook wb = Workbook.getWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
Sheet sheet = wb.getSheet("Sheet1");
Cell cell = sheet.getCell(0, 0);
assertEquals("Test Template American English", cell.getContents());
}
@Test
public void testJExcelWithTemplateAndLanguage() throws Exception {
request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE,
newDummyLocaleResolver("de", ""));
AbstractJExcelView excelView = new UnixSafeAbstractJExcelView() {
@Override
protected void buildExcelDocument(Map<String, Object> model, WritableWorkbook wb,
HttpServletRequest request, HttpServletResponse response) throws Exception {
WritableSheet sheet = wb.getSheet("Sheet1");
// test all possible permutation of row or column not existing
sheet.addCell(new Label(2, 4, "Test Value"));
sheet.addCell(new Label(2, 3, "Test Value"));
sheet.addCell(new Label(3, 4, "Test Value"));
sheet.addCell(new Label(2, 4, "Test Value"));
}
};
excelView.setApplicationContext(webAppCtx);
excelView.setUrl("template");
excelView.render(new HashMap<String, Object>(), request, response);
Workbook wb = Workbook.getWorkbook(new ByteArrayInputStream(response.getContentAsByteArray()));
Sheet sheet = wb.getSheet("Sheet1");
Cell cell = sheet.getCell(0, 0);
assertEquals("Test Template auf Deutsch", cell.getContents());
}
private LocaleResolver newDummyLocaleResolver(final String lang, final String country) {
return new LocaleResolver() {
@Override
public Locale resolveLocale(HttpServletRequest request) {
return new Locale(lang, country);
}
@Override
public void setLocale(HttpServletRequest request, HttpServletResponse response, Locale locale) {
// not supported
}
};
}
/**
* Workaround JXL bug that causes ArrayIndexOutOfBounds exceptions when running in
* *nix machines. Same bug as reported at http://jira.pentaho.com/browse/PDI-5031.
* <p>We want to use the latest JXL code because it doesn't include log4j config files
* inside the jar. Since the project appears to be abandoned, AbstractJExcelView is
* deprecated as of Spring 4.0.
*/
private static abstract class UnixSafeAbstractJExcelView extends AbstractJExcelView {
@Override
protected Workbook getTemplateSource(String url, HttpServletRequest request) throws Exception {
Workbook workbook = super.getTemplateSource(url, request);
Field field = WorkbookParser.class.getDeclaredField("settings");
field.setAccessible(true);
WorkbookSettings settings = (WorkbookSettings) ReflectionUtils.getField(field, workbook);
settings.setWriteAccess(null);
return workbook;
}
}
}

View File

@@ -8,31 +8,22 @@
<mvc:annotation-driven />
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
<ref bean="log4jInterceptor"/>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/admin/**" />
<mvc:exclude-mapping path="/images/**" />
<bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/logged/**" />
<mvc:mapping path="/foo/logged" />
<ref bean="log4jInterceptor"/>
<bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<bean id="log4jInterceptor"
class="org.springframework.web.context.request.Log4jNestedDiagnosticContextInterceptor" />
<mvc:interceptors path-matcher="pathMatcher">
<mvc:interceptor>
<mvc:mapping path="/accounts/[0-9]*" />
<bean class="org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor" />
<bean class="org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<bean id="pathMatcher" class="org.springframework.web.servlet.config.MvcNamespaceTests$TestPathMatcher" />
<bean id="pathMatcher" class="org.springframework.web.servlet.config.MvcNamespaceTests$TestPathMatcher"/>
</beans>