Skip to content

Commit bc27b38

Browse files
committed
test fixes
1 parent 0f589e8 commit bc27b38

File tree

5 files changed

+257
-29
lines changed

5 files changed

+257
-29
lines changed

logicaldoc-webapp/src/test/java/com/logicaldoc/web/service/DocumentServiceImplTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -877,14 +877,14 @@ public void testDelete() throws ServerException, PersistenceException {
877877

878878
@Test
879879
public void testDeleteNotes() throws ServerException, PersistenceException {
880-
List<DocumentNote> notes = noteDao.findByDocId(1, "1.0");
880+
List<DocumentNote> notes = noteDao.findByDocId(1L, "1.0");
881881
assertNotNull(notes);
882882
assertEquals(2, notes.size());
883883
assertEquals("message for note 1", notes.get(0).getMessage());
884884

885-
testSubject.deleteNotes(List.of(1L));
885+
testSubject.deleteNotes(List.of(notes.get(0).getId()));
886886

887-
notes = noteDao.findByDocId(1, "1.0");
887+
notes = noteDao.findByDocId(1L, "1.0");
888888
assertNotNull(notes);
889889
assertEquals(1, notes.size());
890890
}

logicaldoc-webservice/pom.xml

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,6 @@
1111
<name>logicaldoc-webservice</name>
1212
<packaging>jar</packaging>
1313
<dependencies>
14-
<dependency>
15-
<groupId>com.logicaldoc</groupId>
16-
<artifactId>logicaldoc-webapp</artifactId>
17-
<version>${project.version}</version>
18-
<classifier>api</classifier>
19-
<scope>provided</scope>
20-
<exclusions>
21-
<exclusion>
22-
<groupId>xml-apis</groupId>
23-
<artifactId>xml-apis</artifactId>
24-
</exclusion>
25-
<exclusion>
26-
<groupId>xml-apis</groupId>
27-
<artifactId>xml-apis-ext</artifactId>
28-
</exclusion>
29-
</exclusions>
30-
</dependency>
3114
<dependency>
3215
<groupId>com.logicaldoc</groupId>
3316
<artifactId>logicaldoc-core</artifactId>
Lines changed: 107 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,130 @@
11
package com.logicaldoc.webservice;
22

3-
import com.logicaldoc.web.ChartServlet;
3+
import java.awt.Color;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.text.DateFormat;
7+
import java.text.SimpleDateFormat;
8+
import java.util.Calendar;
9+
import java.util.Locale;
10+
11+
import javax.servlet.ServletException;
12+
import javax.servlet.http.HttpServlet;
13+
import javax.servlet.http.HttpServletRequest;
14+
import javax.servlet.http.HttpServletResponse;
15+
16+
import org.jfree.chart.ChartFactory;
17+
import org.jfree.chart.ChartUtilities;
18+
import org.jfree.chart.JFreeChart;
19+
import org.jfree.chart.axis.CategoryAxis;
20+
import org.jfree.chart.axis.CategoryLabelPositions;
21+
import org.jfree.chart.axis.NumberAxis;
22+
import org.jfree.chart.plot.CategoryPlot;
23+
import org.jfree.chart.plot.PlotOrientation;
24+
import org.jfree.chart.renderer.category.BarRenderer;
25+
import org.jfree.data.category.DefaultCategoryDataset;
26+
import org.jfree.ui.HorizontalAlignment;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
30+
import com.logicaldoc.core.security.Tenant;
31+
import com.logicaldoc.core.sequence.SequenceDAO;
32+
import com.logicaldoc.util.Context;
33+
import com.logicaldoc.util.LocaleUtil;
34+
import com.logicaldoc.util.io.FileUtil;
435

536
/**
637
* This servlet provides the chart of the calls to the API
738
*
839
* @author Marco Meschieri - LogicalDOC
940
* @since 8.6.1
1041
*/
11-
public class WebserviceChartServlet extends ChartServlet {
42+
public class WebserviceChartServlet extends HttpServlet {
43+
44+
public static final String USER_ID = "userId";
1245

1346
private static final long serialVersionUID = -6956612970433309888L;
1447

48+
private static final Logger log = LoggerFactory.getLogger(WebserviceChartServlet.class);
49+
1550
/**
1651
* Constructor of the object.
1752
*/
1853
public WebserviceChartServlet() {
1954
super();
2055
}
2156

57+
/**
58+
* The doGet method of the servlet. <br>
59+
*
60+
* This method is called when a form has its tag value method equals to get.
61+
*
62+
* @param request the request send by the client to the server
63+
* @param response the response send by the server to the client
64+
* @throws ServletException if an error occurred
65+
* @throws IOException if an error occurred
66+
*/
2267
@Override
23-
protected String sequencePrefix() {
24-
return WebserviceInterceptor.WSCALL;
25-
}
68+
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
69+
File chartFile = null;
70+
try {
71+
WebserviceServletUtil.validateSession(request);
2672

27-
@Override
28-
protected String messageTitle() {
29-
return "calls";
73+
int width = 1200;
74+
if (request.getParameter("width") != null)
75+
width = Integer.parseInt(request.getParameter("width"));
76+
77+
int height = 450;
78+
if (request.getParameter("height") != null)
79+
height = Integer.parseInt(request.getParameter("height"));
80+
81+
Locale locale = Locale.ENGLISH;
82+
if (request.getParameter("locale") != null)
83+
locale = LocaleUtil.toLocale(request.getParameter("locale"));
84+
85+
long tenantId = Tenant.SYSTEM_ID;
86+
if (request.getParameter("tenantId") != null)
87+
tenantId = Long.parseLong(request.getParameter("tenantId"));
88+
89+
chartFile = FileUtil.createTempFile("chart", ".png");
90+
91+
/**
92+
* Retrieve the sequences and order them by date
93+
*/
94+
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
95+
SequenceDAO dao = Context.get(SequenceDAO.class);
96+
97+
DateFormat dfName = new SimpleDateFormat("MMM yyyy", locale);
98+
DateFormat dfNumber = new SimpleDateFormat("yyyyMM");
99+
for (int i = 0; i < 36; i++) {
100+
Calendar cal = Calendar.getInstance();
101+
cal.add(Calendar.MONTH, -i);
102+
String month = dfNumber.format(cal.getTime());
103+
String monthName = dfName.format(cal.getTime());
104+
dataset.addValue(dao.getCurrentValue(WebserviceInterceptor.WSCALL_HYPHEN + month, 0L, tenantId),
105+
"calls", monthName);
106+
}
107+
108+
JFreeChart chart = ChartFactory.createBarChart("", null, null, dataset, PlotOrientation.VERTICAL, false,
109+
false, false);
110+
111+
CategoryPlot catPlot = chart.getCategoryPlot();
112+
catPlot.getRangeAxis().setStandardTickUnits(NumberAxis.createIntegerTickUnits());
113+
BarRenderer r = (BarRenderer) catPlot.getRenderer();
114+
r.setSeriesPaint(0, Color.BLUE);
115+
116+
chart.getTitle().setHorizontalAlignment(HorizontalAlignment.LEFT);
117+
chart.getTitle().setPaint(Color.BLUE);
118+
119+
CategoryAxis domainAxis = catPlot.getDomainAxis();
120+
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
121+
122+
ChartUtilities.saveChartAsPNG(chartFile, chart, width, height);
123+
WebserviceServletUtil.downloadFile(request, response, chartFile, "wschart.png");
124+
} catch (Exception e) {
125+
log.error(e.getMessage(), e);
126+
} finally {
127+
FileUtil.delete(chartFile);
128+
}
30129
}
31130
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
package com.logicaldoc.webservice;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.OutputStream;
9+
import java.io.UnsupportedEncodingException;
10+
import java.net.URLEncoder;
11+
import java.nio.charset.StandardCharsets;
12+
13+
import javax.servlet.ServletException;
14+
import javax.servlet.http.HttpServletRequest;
15+
import javax.servlet.http.HttpServletResponse;
16+
17+
import org.apache.commons.codec.binary.Base64;
18+
import org.apache.commons.io.IOUtils;
19+
20+
import com.logicaldoc.core.security.Session;
21+
import com.logicaldoc.core.security.SessionManager;
22+
import com.logicaldoc.util.MimeType;
23+
24+
/**
25+
* Some methods useful in webservice servlets
26+
*
27+
* @author Marco Meschieri - LogicalDOC
28+
* @since 8.7
29+
*/
30+
public class WebserviceServletUtil {
31+
32+
private static final String UTF_8 = "UTF-8";
33+
34+
private static final int DEFAULT_BUFFER_SIZE = 10240; // ..bytes = 10KB.
35+
36+
private WebserviceServletUtil() {
37+
}
38+
39+
/**
40+
* Sets the correct Content-Disposition header into the response
41+
*
42+
* @param request the HTTP request
43+
* @param response the server's response
44+
* @param filename name of the file
45+
*
46+
* @throws UnsupportedEncodingException error trying to encode the response
47+
*/
48+
public static void setContentDisposition(HttpServletRequest request, HttpServletResponse response, String filename)
49+
throws UnsupportedEncodingException {
50+
// Encode the filename
51+
String userAgent = request.getHeader("User-Agent").toLowerCase();
52+
53+
String encodedFileName = null;
54+
if (userAgent.contains("msie") || userAgent.contains("opera")
55+
|| (userAgent.contains("trident") && userAgent.contains("windows"))
56+
|| (userAgent.contains("edge") && userAgent.contains("windows"))) {
57+
encodedFileName = URLEncoder.encode(filename, UTF_8);
58+
encodedFileName = encodedFileName.replace("+", "%20");
59+
} else if (userAgent.contains("safari") && !userAgent.contains("chrome")) {
60+
// Safari User-Agent contains "chrome"
61+
encodedFileName = filename;
62+
} else if (userAgent.contains("safari") && userAgent.contains("chrome") && userAgent.contains("android")) {
63+
// Used by some LG phones
64+
encodedFileName = filename;
65+
} else {
66+
encodedFileName = "=?UTF-8?B?"
67+
+ new String(Base64.encodeBase64(filename.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8)
68+
+ "?=";
69+
}
70+
71+
boolean asAttachment = true;
72+
if (request.getParameter("open") != null)
73+
asAttachment = !"true".equals(request.getParameter("open"));
74+
else if (request.getAttribute("open") != null)
75+
asAttachment = !"true".equals(request.getAttribute("open"));
76+
77+
response.setHeader("Content-Disposition",
78+
(asAttachment ? "attachment" : "inline") + "; filename=\"" + encodedFileName + "\"");
79+
80+
// Avoid resource caching
81+
response.setHeader("Cache-Control", "no-cache,no-store,must-revalidate");
82+
response.setHeader("Expires", "0");
83+
response.setHeader("Pragma", "no-cache");
84+
}
85+
86+
/**
87+
* Sends the specified file to the response object; the client will receive
88+
* it as a download
89+
*
90+
* Sends the specified file to the response object; the client will receive
91+
* it as a download
92+
*
93+
* @param request the current request
94+
* @param response the file is written to this object
95+
* @param file file to serve
96+
* @param fileName client file name
97+
*
98+
* @throws IOException generic I/O error
99+
*/
100+
public static void downloadFile(HttpServletRequest request, HttpServletResponse response, File file,
101+
String fileName) throws IOException {
102+
103+
String filename = fileName;
104+
if (filename == null)
105+
filename = file.getName();
106+
107+
// get the mimetype
108+
String mimetype = MimeType.getByFilename(filename);
109+
// it seems everything is fine, so we can now start writing to the
110+
// response object
111+
response.setContentType(mimetype);
112+
setContentDisposition(request, response, filename);
113+
114+
// Add this header for compatibility with internal .NET browsers
115+
response.setHeader("Content-Length", Long.toString(file.length()));
116+
117+
try (InputStream is = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
118+
OutputStream os = response.getOutputStream();) {
119+
IOUtils.copy(is, os);
120+
}
121+
}
122+
123+
public static Session validateSession(HttpServletRequest request) throws ServletException {
124+
String sid = SessionManager.get().getSessionId(request);
125+
return validateSession(sid);
126+
}
127+
128+
/**
129+
* Throws a runtime exception id the given session is invalid
130+
*
131+
* @param sid identifier of the session
132+
*
133+
* @return the session
134+
*
135+
* @throws ServletException the session does not exist or is expired
136+
*/
137+
public static Session validateSession(String sid) throws ServletException {
138+
Session session = SessionManager.get().get(sid);
139+
if (session == null)
140+
throw new ServletException("Invalid Session");
141+
if (!SessionManager.get().isOpen(sid))
142+
throw new ServletException("Invalid or Expired Session");
143+
SessionManager.get().renew(sid);
144+
return session;
145+
}
146+
}

logicaldoc-webservice/src/test/java/com/logicaldoc/webservice/soap/endpoint/SoapDocumentServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public void testDeleteVersion() throws PermissionException, PersistenceException
230230
while (version == null)
231231
version = testSubject.getVersion(session.getSid(), wsDoc.getId(), "2.0");
232232

233-
assertEquals(1, testSubject.getVersions(session.getSid(), wsDoc.getId()).size());
233+
assertEquals(2, testSubject.getVersions(session.getSid(), wsDoc.getId()).size());
234234

235235
testSubject.deleteVersion(session.getSid(), wsDoc.getId(), "2.0");
236236
assertEquals(1, testSubject.getVersions(session.getSid(), wsDoc.getId()).size());

0 commit comments

Comments
 (0)