Bonjour à tous,

Je recherche en vain depuis ce matin un ebooks/tuto/etc... portant sur les test unitaires de servlet.

Je ne cherche pas vraiment de tools ultra performant mais simplement une manière de tester ce genre de servlet assez simple.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
 
package com.xxx.xxx.util;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ResourceBundle;
 
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.version.Version;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.io.IOUtils;
import javax.activation.MimetypesFileTypeMap;
 
 
/**
 * The Class LivelinkDataContentServlet.
 */
public class LivelinkDataContentServlet {
 
	/** The log. */
	private static Log log = LogFactory.getLog(LivelinkDataContentServletTest.class);
 
	/** The field. */
	private static String field = null;
 
	/** The bundle. */
	private static ResourceBundle bundle = null;
 
	/** The livelink connection util. */
	private static LivelinkConnectionUtil livelinkConnectionUtil = null;
 
	/**
         * Inits the servlet. Specify in web xml init parameter value for fieldName.
         * It's the name of input field in web page. <code><init-param>
         * <param-name>uuidName</param-name>
         * <param-value>uuid</param-value>
         * <description>The name of input field</description>
         * </init-param></code>
         * 
         * @param config
         *            the config
         * 
         * @throws ServletException
         *             the servlet exception
         */
	public void init(ServletConfig config) throws ServletException {
 
		if (field == null) {
			field = config.getInitParameter("uuidName");
		}
		if (bundle == null) {
			bundle = ResourceBundle.getBundle("livelink");
		}
		if (livelinkConnectionUtil == null) {
			livelinkConnectionUtil = new LivelinkConnectionUtil(bundle);	
		}
	}
 
	/**
         * Do get.
         * 
         * @param request
         *            the request
         * @param response
         *            the response
         * 
         * @throws ServletException
         *             the servlet exception
         * @throws IOException
         *             Signals that an I/O exception has occurred.
         * 
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
         *      response)
         */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
		String objectId = request.getParameter(field);
 
		Node document = null;
		byte[] contentData = null;
		String documentName = null;
		String mimeType = null;
		try {
			document = getDocumentByUuid(objectId);
			contentData = getContentDataForDocument(document);
			documentName = document.getName();
 
		} catch (RepositoryException e) {
			response.sendError(HttpServletResponse.SC_NOT_FOUND);
		}
 
		if (document != null) {
			File tempFile = null;
			try {
				tempFile = createTempFile(documentName, contentData);
			} catch (IOException iox) {
				response.sendError(HttpServletResponse.SC_NOT_FOUND);
			}
			mimeType = new MimetypesFileTypeMap().getContentType(tempFile);
			response.setContentType(mimeType); 
			response.setContentLength((int) tempFile.length());
 
			response.setHeader("Content-Disposition", "attachment; filename=" + documentName);
			response.setHeader("Cache-Control", "private, must-revalidate");	
 
			OutputStream out = response.getOutputStream();
			try {
				out.write(contentData);
				out.flush();
			} catch (IOException e) {
				response.sendError(HttpServletResponse.SC_NOT_FOUND);
			} finally {
				try {
					out.close();
					tempFile.delete();
				} catch (IOException e) {
					throw e;
				}
			}
		}
	}
 
 
	/**
         * Creates the temp file.
         * 
         * @param fileName
         *            the file name
         * @param contentData
         *            the content data
         * 
         * @return the file
         * 
         * @throws IOException
         *             Signals that an I/O exception has occurred.
         */
	private File createTempFile(String fileName, byte[] contentData) throws IOException {
		Date date  = new Date();
		SimpleDateFormat format = new SimpleDateFormat("ddMMMyyyyHHmmss");
		File file = new File(fileName + format.format(date));
		log.info(file.getAbsolutePath());
 
		OutputStream out = null;
		try {
			out = new FileOutputStream(file);
			out.write(contentData);
			out.flush();
		} catch (IOException e) {
			throw e;
 
		} finally {
			if (out != null) {
				try {
					out.close();
				} catch (IOException iox) {
					log.warn(iox);
				}
			}			
		}
 
 
		return file;
	}
 
	/**
         * Gets the document by uuid.
         * 
         * @param uuid
         *            the uuid
         * 
         * @return the document by uuid
         * 
         * @throws RepositoryException
         *             the repository exception
         */
	private Node getDocumentByUuid(String uuid) throws RepositoryException {
 
		Session session = livelinkConnectionUtil.getSession(LivelinkConnectionUtil.ENTERPRISE_WORKSPACE);
		return session.getNodeByUUID(uuid);
 
	}
 
	/**
         * Gets the content data for document.
         * 
         * @param document
         *            the document
         * 
         * @return the content data for document
         * 
         * @throws RepositoryException
         *             the repository exception
         * @throws IOException
         *             Signals that an I/O exception has occurred.
         */
	private byte[] getContentDataForDocument(Node document) throws RepositoryException, IOException {
 
		byte[] contentByte = null;
		InputStream in = null;
 
		try {
 
			if (document != null) {
				Version version = document.getBaseVersion();
 
				if (version != null) {
					in = version.getProperty("ll:contentData").getStream();
					if (in != null) {
						contentByte = IOUtils.toByteArray(in);
					}
				}
			}			
		} catch (RepositoryException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} finally {
			if (in != null) {
				in.close();
			}
		}
 
		return contentByte;
	}
 
	/**
         * Do post.
         * 
         * @param request
         *            the request
         * @param response
         *            the response
         * 
         * @throws ServletException
         *             the servlet exception
         * @throws IOException
         *             Signals that an I/O exception has occurred.
         * 
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
         *      response)
         */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}	
 
 
}
En vous remerciant d'avance.