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+ }
0 commit comments