WebBox Klassenbibliothek
ulrich
2017-12-27 5bfcdd214e1e6b3b7653009137d316dabadcda91
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
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.uhilger.wbx.web;
 
import java.io.IOException;
import java.security.Principal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import org.apache.catalina.servlets.DefaultServlet;
 
/**
 *
 * @author ulrich
 */
public class WbxServlet extends DefaultServlet {
  
  protected static final Logger logger = Logger.getLogger(WbxServlet.class.getName());
  protected static final String HOME_CTX = "/home";
  
  
  protected String getUrlUser(HttpServletRequest request, String userName) throws IOException {
    String result = "";
    String requestUrlStr = request.getRequestURL().toString();
    String contextPath = request.getContextPath();
    if(contextPath != null && requestUrlStr != null && userName != null) {
      int start = requestUrlStr.indexOf(contextPath);
      start += contextPath.length();
      start++;
      int end = start + userName.length();
      try {
        result = requestUrlStr.substring(start, end);
      } catch(Exception ex) {
        logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex);
      }
    }
    return result;
  }
  
  protected String getUserName(HttpServletRequest hr) {
    String userName = null;
    Object p = hr.getUserPrincipal();
    if (p instanceof Principal) {
      userName = ((Principal) p).getName();
    }
    return userName;
  }
 
}