Revision 31e19588

b/src/gr/ebs/gss/server/rest/FilesHandler.java
36 36
import gr.ebs.gss.server.ejb.ExternalAPI;
37 37
import gr.ebs.gss.server.ejb.TransactionHelper;
38 38
import gr.ebs.gss.server.webdav.Range;
39
import gr.ebs.gss.server.webdav.RequestUtil;
39 40

  
40 41
import java.io.BufferedReader;
41 42
import java.io.ByteArrayInputStream;
......
58 59
import java.util.Collection;
59 60
import java.util.Date;
60 61
import java.util.HashSet;
62
import java.util.Iterator;
61 63
import java.util.List;
62 64
import java.util.Set;
63 65
import java.util.StringTokenizer;
......
136 138
	private ServletContext context;
137 139

  
138 140
	/**
141
	 * The style sheet for displaying the directory listings.
142
	 */
143
	private static final String GSS_CSS = "H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} " + "H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} " + "H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} " + "BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} " + "B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} " + "P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}" + "A {color : black;}" + "A.name {color : black;}" + "HR {color : #525D76;}";
144

  
145

  
146
	/**
139 147
	 * @param servletContext
140 148
	 */
141 149
	public FilesHandler(ServletContext servletContext) {
......
405 413
        		file.setMimeType(contentType);
406 414
        	}
407 415
    	}
408
    	else if(req.getHeader("Accept").contains("text/html")){
409
    		if(folder != null && folder.isReadForAll()){
410
				contentType = "text/html";
416
    	else if (req.getHeader("Accept").contains("text/html"))
417
			if(folder != null && folder.isReadForAll()){
418
				contentType = "text/html;charset=UTF-8";
411 419
				isFolderPublic = true;
412 420
    		}
413
    	}
414
		else
415
			contentType = "application/json;charset=UTF-8";
421
			else
422
        		contentType = "application/json;charset=UTF-8";
423
    	else
424
    		contentType = "application/json;charset=UTF-8";
425

  
416 426

  
417 427
    	ArrayList ranges = null;
418 428
    	long contentLength = -1L;
......
487 497
    		}
488 498

  
489 499
    		InputStream renderResult = null;
490
    		if (isFolderPublic)
491
				renderResult = renderHtml(req.getContextPath(), path, folder,user,context, req);
492
			else if (content)
493
				// Serve the directory browser
494
				try {
495
					renderResult = renderJson(user, folder);
496
				} catch (InsufficientPermissionsException e) {
497
					resp.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
498
			    	return;
499
				}
500
    		if (content)
501
    			// Serve the directory browser for a public folder
502
    			if (isFolderPublic)
503
    				renderResult = renderHtml(req.getContextPath(), path, folder,user);
504
    			// Serve the directory for an ordinary folder
505
    			else
506
    				try {
507
    					renderResult = renderJson(user, folder);
508
    					} catch (InsufficientPermissionsException e) {
509
    						resp.setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
510
    						return;
511
    					}
512

  
513

  
500 514
    		// Copy the input stream to our output stream (if requested)
501 515
    		if (content) {
502 516
    			try {
......
2157 2171
				}
2158 2172
		}
2159 2173
	}
2174
	/**
2175
	 * Return an InputStream to an HTML representation of the contents of this
2176
	 * directory.
2177
	 *
2178
	 * @param contextPath Context path to which our internal paths are relative
2179
	 * @param path the requested path to the resource
2180
	 * @param folder the specified directory
2181
	 * @param req the HTTP request
2182
	 * @return an input stream with the rendered contents
2183
	 * @throws IOException
2184
	 * @throws ServletException
2185
	 */
2186
	private InputStream renderHtml(String contextPath, String path, FolderDTO folder, User user) throws IOException, ServletException {
2187
		String name = folder.getName();
2188
		// Prepare a writer to a buffered area
2189
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
2190
		OutputStreamWriter osWriter = new OutputStreamWriter(stream, "UTF8");
2191
		PrintWriter writer = new PrintWriter(osWriter);
2192
		StringBuffer sb = new StringBuffer();
2193
		// rewriteUrl(contextPath) is expensive. cache result for later reuse
2194
		String rewrittenContextPath = rewriteUrl(contextPath);
2195
		// Render the page header
2196
		sb.append("<html>\r\n");
2197
		sb.append("<head>\r\n");
2198
		sb.append("<title>");
2199
		sb.append("Index of " + name);
2200
		sb.append("</title>\r\n");
2201
		sb.append("<STYLE><!--");
2202
		sb.append(GSS_CSS);
2203
		sb.append("--></STYLE> ");
2204
		sb.append("</head>\r\n");
2205
		sb.append("<body>");
2206
		sb.append("<h1>");
2207
		sb.append("Index of " + name);
2208

  
2209
		// Render the link to our parent (if required)
2210
		String parentDirectory = path;
2211
		if (parentDirectory.endsWith("/"))
2212
			parentDirectory = parentDirectory.substring(0, parentDirectory.length() - 1);
2213
		int slash = parentDirectory.lastIndexOf('/');
2214
		if (slash >= 0) {
2215
			String parent = path.substring(0, slash);
2216
			sb.append(" - <a href=\"");
2217
			sb.append(rewrittenContextPath);
2218
			if (parent.equals(""))
2219
				parent = "/";
2220
			sb.append(rewriteUrl(parent));
2221
			if (!parent.endsWith("/"))
2222
				sb.append("/");
2223
			sb.append("\">");
2224
			sb.append("<b>");
2225
			sb.append("Up To " + parent);
2226
			sb.append("</b>");
2227
			sb.append("</a>");
2228
		}
2229

  
2230
		sb.append("</h1>");
2231
		sb.append("<HR size=\"1\" noshade=\"noshade\">");
2232

  
2233
		sb.append("<table width=\"100%\" cellspacing=\"0\"" + " cellpadding=\"5\" align=\"center\">\r\n");
2234

  
2235
		// Render the column headings
2236
		sb.append("<tr>\r\n");
2237
		sb.append("<td align=\"left\"><font size=\"+1\"><strong>");
2238
		sb.append("Name");
2239
		sb.append("</strong></font></td>\r\n");
2240
		sb.append("<td align=\"center\"><font size=\"+1\"><strong>");
2241
		sb.append("Size");
2242
		sb.append("</strong></font></td>\r\n");
2243
		sb.append("<td align=\"right\"><font size=\"+1\"><strong>");
2244
		sb.append("Last modified");
2245
		sb.append("</strong></font></td>\r\n");
2246
		sb.append("</tr>");
2247
		// Render the directory entries within this directory
2248
		boolean shade = false;
2249
		Iterator iter = folder.getSubfolders().iterator();
2250
		while (iter.hasNext()) {
2251
			FolderDTO subf = (FolderDTO) iter.next();
2252
			String resourceName = subf.getName();
2253
			if (resourceName.equalsIgnoreCase("WEB-INF") || resourceName.equalsIgnoreCase("META-INF"))
2254
				continue;
2255

  
2256
			sb.append("<tr");
2257
			if (shade)
2258
				sb.append(" bgcolor=\"#eeeeee\"");
2259
			sb.append(">\r\n");
2260
			shade = !shade;
2261

  
2262
			sb.append("<td align=\"left\">&nbsp;&nbsp;\r\n");
2263
			sb.append("<a href=\"");
2264
			sb.append(rewrittenContextPath);
2265
			sb.append(rewriteUrl(path + resourceName));
2266
			sb.append("/");
2267
			sb.append("\"><tt>");
2268
			sb.append(RequestUtil.filter(resourceName));
2269
			sb.append("/");
2270
			sb.append("</tt></a></td>\r\n");
2271

  
2272
			sb.append("<td align=\"right\"><tt>");
2273
			sb.append("&nbsp;");
2274
			sb.append("</tt></td>\r\n");
2275

  
2276
			sb.append("<td align=\"right\"><tt>");
2277
			sb.append(getLastModifiedHttp(folder.getAuditInfo()));
2278
			sb.append("</tt></td>\r\n");
2279

  
2280
			sb.append("</tr>\r\n");
2281
		}
2282
		List<FileHeaderDTO> files;
2283
		try {
2284
			files = getService().getFiles(user.getId(), folder.getId(), true);
2285
		} catch (ObjectNotFoundException e) {
2286
			throw new ServletException(e.getMessage());
2287
		} catch (InsufficientPermissionsException e) {
2288
			throw new ServletException(e.getMessage());
2289
		} catch (RpcException e) {
2290
			throw new ServletException(e.getMessage());
2291
		}
2292
		for (FileHeaderDTO file : files) {
2293
			String resourceName = file.getName();
2294
			if (resourceName.equalsIgnoreCase("WEB-INF") || resourceName.equalsIgnoreCase("META-INF"))
2295
				continue;
2296

  
2297
			sb.append("<tr");
2298
			if (shade)
2299
				sb.append(" bgcolor=\"#eeeeee\"");
2300
			sb.append(">\r\n");
2301
			shade = !shade;
2302

  
2303
			sb.append("<td align=\"left\">&nbsp;&nbsp;\r\n");
2304
			sb.append("<a href=\"");
2305
			sb.append(rewrittenContextPath);
2306
			sb.append(rewriteUrl(path + resourceName));
2307
			sb.append("\"><tt>");
2308
			sb.append(RequestUtil.filter(resourceName));
2309
			sb.append("</tt></a></td>\r\n");
2310

  
2311
			sb.append("<td align=\"right\"><tt>");
2312
			sb.append(renderSize(file.getFileSize()));
2313
			sb.append("</tt></td>\r\n");
2314

  
2315
			sb.append("<td align=\"right\"><tt>");
2316
			sb.append(getLastModifiedHttp(file.getAuditInfo()));
2317
			sb.append("</tt></td>\r\n");
2318

  
2319
			sb.append("</tr>\r\n");
2320
		}
2321

  
2322
		// Render the page footer
2323
		sb.append("</table>\r\n");
2324

  
2325
		sb.append("<HR size=\"1\" noshade=\"noshade\">");
2326

  
2327
		//sb.append("<h3>").append(getServletContext().getServerInfo()).append("</h3>");
2328
		sb.append("</body>\r\n");
2329
		sb.append("</html>\r\n");
2330

  
2331
		// Return an input stream to the underlying bytes
2332
		writer.write(sb.toString());
2333
		writer.flush();
2334
		return new ByteArrayInputStream(stream.toByteArray());
2335

  
2336
	}
2160 2337
}
b/src/gr/ebs/gss/server/webdav/Webdav.java
1555 1555
	 * @param path Path which has to be rewritten
1556 1556
	 * @return the rewritten URL
1557 1557
	 */
1558
	private String rewriteUrl(String path) {
1558
	protected String rewriteUrl(String path) {
1559 1559
		return urlEncoder.encode(path);
1560 1560
	}
1561 1561

  
......
2855 2855
	 * @param size File size (in bytes)
2856 2856
	 * @return the size as a string
2857 2857
	 */
2858
	private String renderSize(long size) {
2858
	protected String renderSize(long size) {
2859 2859
		long leftSide = size / 1024;
2860 2860
		long rightSide = size % 1024 / 103; // Makes 1 digit
2861 2861
		if (leftSide == 0 && rightSide == 0 && size > 0)
......
3451 3451
		return true;
3452 3452
	}
3453 3453

  
3454
	/**
3455
	 * Return an InputStream to an HTML representation of the contents of this
3456
	 * directory.
3457
	 *
3458
	 * @param contextPath Context path to which our internal paths are relative
3459
	 * @param path the requested path to the resource
3460
	 * @param folder the specified directory
3461
	 * @param req the HTTP request
3462
	 * @return an input stream with the rendered contents
3463
	 * @throws IOException
3464
	 * @throws ServletException
3465
	 */
3466
	protected InputStream renderHtml(String contextPath, String path, FolderDTO folder, User user,ServletContext context, @SuppressWarnings("unused") HttpServletRequest req) throws IOException, ServletException {
3467
		String name = folder.getName();
3468
		// Prepare a writer to a buffered area
3469
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
3470
		OutputStreamWriter osWriter = new OutputStreamWriter(stream, "UTF8");
3471
		PrintWriter writer = new PrintWriter(osWriter);
3472
		StringBuffer sb = new StringBuffer();
3473
		// rewriteUrl(contextPath) is expensive. cache result for later reuse
3474
		String rewrittenContextPath = rewriteUrl(contextPath);
3475
		// Render the page header
3476
		sb.append("<html>\r\n");
3477
		sb.append("<head>\r\n");
3478
		sb.append("<title>");
3479
		sb.append("Index of " + name);
3480
		sb.append("</title>\r\n");
3481
		sb.append("<STYLE><!--");
3482
		sb.append(GSS_CSS);
3483
		sb.append("--></STYLE> ");
3484
		sb.append("</head>\r\n");
3485
		sb.append("<body>");
3486
		sb.append("<h1>");
3487
		sb.append("Index of " + name);
3488

  
3489
		// Render the link to our parent (if required)
3490
		String parentDirectory = path;
3491
		if (parentDirectory.endsWith("/"))
3492
			parentDirectory = parentDirectory.substring(0, parentDirectory.length() - 1);
3493
		int slash = parentDirectory.lastIndexOf('/');
3494
		if (slash >= 0) {
3495
			String parent = path.substring(0, slash);
3496
			sb.append(" - <a href=\"");
3497
			sb.append(rewrittenContextPath);
3498
			if (parent.equals(""))
3499
				parent = "/";
3500
			sb.append(rewriteUrl(parent));
3501
			if (!parent.endsWith("/"))
3502
				sb.append("/");
3503
			sb.append("\">");
3504
			sb.append("<b>");
3505
			sb.append("Up To " + parent);
3506
			sb.append("</b>");
3507
			sb.append("</a>");
3508
		}
3509

  
3510
		sb.append("</h1>");
3511
		sb.append("<HR size=\"1\" noshade=\"noshade\">");
3512

  
3513
		sb.append("<table width=\"100%\" cellspacing=\"0\"" + " cellpadding=\"5\" align=\"center\">\r\n");
3514

  
3515
		// Render the column headings
3516
		sb.append("<tr>\r\n");
3517
		sb.append("<td align=\"left\"><font size=\"+1\"><strong>");
3518
		sb.append("Name");
3519
		sb.append("</strong></font></td>\r\n");
3520
		sb.append("<td align=\"center\"><font size=\"+1\"><strong>");
3521
		sb.append("Size");
3522
		sb.append("</strong></font></td>\r\n");
3523
		sb.append("<td align=\"right\"><font size=\"+1\"><strong>");
3524
		sb.append("Last modified");
3525
		sb.append("</strong></font></td>\r\n");
3526
		sb.append("</tr>");
3527
		// Render the directory entries within this directory
3528
		boolean shade = false;
3529
		Iterator iter = folder.getSubfolders().iterator();
3530
		while (iter.hasNext()) {
3531
			FolderDTO subf = (FolderDTO) iter.next();
3532
			String resourceName = subf.getName();
3533
			if (resourceName.equalsIgnoreCase("WEB-INF") || resourceName.equalsIgnoreCase("META-INF"))
3534
				continue;
3535

  
3536
			sb.append("<tr");
3537
			if (shade)
3538
				sb.append(" bgcolor=\"#eeeeee\"");
3539
			sb.append(">\r\n");
3540
			shade = !shade;
3541

  
3542
			sb.append("<td align=\"left\">&nbsp;&nbsp;\r\n");
3543
			sb.append("<a href=\"");
3544
			sb.append(rewrittenContextPath);
3545
			sb.append(rewriteUrl(path + resourceName));
3546
			sb.append("/");
3547
			sb.append("\"><tt>");
3548
			sb.append(RequestUtil.filter(resourceName));
3549
			sb.append("/");
3550
			sb.append("</tt></a></td>\r\n");
3551

  
3552
			sb.append("<td align=\"right\"><tt>");
3553
			sb.append("&nbsp;");
3554
			sb.append("</tt></td>\r\n");
3555

  
3556
			sb.append("<td align=\"right\"><tt>");
3557
			sb.append(getLastModifiedHttp(folder.getAuditInfo()));
3558
			sb.append("</tt></td>\r\n");
3559

  
3560
			sb.append("</tr>\r\n");
3561
		}
3562
		List<FileHeaderDTO> files;
3563
		try {
3564
			//User user = getUser(req);
3565
			files = getService().getFiles(user.getId(), folder.getId(), true);
3566
		} catch (ObjectNotFoundException e) {
3567
			throw new ServletException(e.getMessage());
3568
		} catch (InsufficientPermissionsException e) {
3569
			throw new ServletException(e.getMessage());
3570
		} catch (RpcException e) {
3571
			throw new ServletException(e.getMessage());
3572
		}
3573
		for (FileHeaderDTO file : files) {
3574
			String resourceName = file.getName();
3575
			if (resourceName.equalsIgnoreCase("WEB-INF") || resourceName.equalsIgnoreCase("META-INF"))
3576
				continue;
3577

  
3578
			sb.append("<tr");
3579
			if (shade)
3580
				sb.append(" bgcolor=\"#eeeeee\"");
3581
			sb.append(">\r\n");
3582
			shade = !shade;
3583

  
3584
			sb.append("<td align=\"left\">&nbsp;&nbsp;\r\n");
3585
			sb.append("<a href=\"");
3586
			sb.append(rewrittenContextPath);
3587
			sb.append(rewriteUrl(path + resourceName));
3588
			sb.append("\"><tt>");
3589
			sb.append(RequestUtil.filter(resourceName));
3590
			sb.append("</tt></a></td>\r\n");
3591

  
3592
			sb.append("<td align=\"right\"><tt>");
3593
			sb.append(renderSize(file.getFileSize()));
3594
			sb.append("</tt></td>\r\n");
3595

  
3596
			sb.append("<td align=\"right\"><tt>");
3597
			sb.append(getLastModifiedHttp(file.getAuditInfo()));
3598
			sb.append("</tt></td>\r\n");
3599

  
3600
			sb.append("</tr>\r\n");
3601
		}
3602

  
3603
		// Render the page footer
3604
		sb.append("</table>\r\n");
3605

  
3606
		sb.append("<HR size=\"1\" noshade=\"noshade\">");
3607

  
3608
		sb.append("<h3>").append(context.getServerInfo()).append("</h3>");
3609
		sb.append("</body>\r\n");
3610
		sb.append("</html>\r\n");
3611

  
3612
		// Return an input stream to the underlying bytes
3613
		writer.write(sb.toString());
3614
		writer.flush();
3615
		return new ByteArrayInputStream(stream.toByteArray());
3616

  
3617
	}
3618

  
3619 3454
}

Also available in: Unified diff