1 | package gov.va.med.edp.web.controller;
|
---|
2 |
|
---|
3 | import gov.va.med.edp.dao.ServerPackageVersionDao;
|
---|
4 | import gov.va.med.edp.dao.SiteCodeLookUpDao;
|
---|
5 | import gov.va.med.edp.dao.TrackingDao;
|
---|
6 |
|
---|
7 | import java.util.Enumeration;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.Map;
|
---|
10 |
|
---|
11 | import javax.servlet.http.HttpServletRequest;
|
---|
12 |
|
---|
13 | import org.apache.log4j.Logger;
|
---|
14 | import org.springframework.util.StringUtils;
|
---|
15 | import org.springframework.web.servlet.ModelAndView;
|
---|
16 | import org.springframework.web.servlet.mvc.AbstractController;
|
---|
17 |
|
---|
18 | public abstract class SiteCodeController extends AbstractController {
|
---|
19 |
|
---|
20 | public static final String MACHINE_NAME_PARAM = "machineName";
|
---|
21 | public static final String SITE_CODE_PARAM = "siteCode";
|
---|
22 | public static final String MACHINE_NAME_HEADER = "EdisClientMachineName";
|
---|
23 |
|
---|
24 | private SiteCodeLookUpDao siteCodeDao;
|
---|
25 | private TrackingDao trackingDao;
|
---|
26 |
|
---|
27 | private String errorViewName;
|
---|
28 | protected ServerPackageVersionDao serverPackageVersionDao;
|
---|
29 |
|
---|
30 | private static Logger log = Logger.getLogger(SiteCodeController.class);
|
---|
31 |
|
---|
32 |
|
---|
33 | protected ModelAndView createErrorModelAndView(String errorMessage) {
|
---|
34 | ModelAndView mav = new ModelAndView(getErrorViewName());
|
---|
35 | mav.addObject("errorMessage", errorMessage);
|
---|
36 | return mav;
|
---|
37 | }
|
---|
38 |
|
---|
39 | public SiteCodeLookUpDao getSiteCodeDao() {
|
---|
40 | return siteCodeDao;
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | public void setSiteCodeDao(SiteCodeLookUpDao siteCodeDao) {
|
---|
45 | this.siteCodeDao = siteCodeDao;
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | public String getErrorViewName() {
|
---|
50 | return errorViewName;
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | public void setErrorViewName(String errorViewName) {
|
---|
55 | this.errorViewName = errorViewName;
|
---|
56 | }
|
---|
57 |
|
---|
58 | protected Map buildParameterMap(HttpServletRequest request, String machineName) {
|
---|
59 | Map params = new HashMap();
|
---|
60 | Enumeration e = request.getParameterNames();
|
---|
61 | while (e.hasMoreElements()) {
|
---|
62 | String paramName = (String) e.nextElement();
|
---|
63 | params.put(paramName, request.getParameter(paramName));
|
---|
64 | }
|
---|
65 | //add the machine name that needs to be passed into M here
|
---|
66 | params.put("machine", machineName.toUpperCase());
|
---|
67 | debug("Parameters:" + params.toString());
|
---|
68 |
|
---|
69 | return params;
|
---|
70 | }
|
---|
71 |
|
---|
72 | protected String parseCNFromHttpHeaders(String machineName){
|
---|
73 | if (StringUtils.hasText(machineName) && machineName.startsWith("CN=")){
|
---|
74 | return machineName.substring(3);
|
---|
75 | }
|
---|
76 | debug("Parsed Machine Name from Http Header is:" + machineName);
|
---|
77 | return machineName;
|
---|
78 | }
|
---|
79 |
|
---|
80 | public TrackingDao getTrackingDao() {
|
---|
81 | return trackingDao;
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | public void setTrackingDao(TrackingDao trackingDao) {
|
---|
86 | this.trackingDao = trackingDao;
|
---|
87 | }
|
---|
88 |
|
---|
89 | private void debug(String s) {
|
---|
90 | if (logger.isDebugEnabled()){
|
---|
91 | logger.debug(s);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | protected String getMachineNameFromUserPrincipalOrSessionOrRequest(HttpServletRequest request) throws UnrecognizedClientException {
|
---|
96 | String machineName = "";
|
---|
97 |
|
---|
98 | //first try user principal. This is for situations where we don't have a load balancer in the mix
|
---|
99 | // this is where the client is talking directly to a weblogic server using 2-way ssl..
|
---|
100 | if (request.getUserPrincipal() != null){
|
---|
101 | machineName = request.getUserPrincipal().getName();
|
---|
102 | debug("Machine name retrieved from user principal is:" + machineName);
|
---|
103 | }
|
---|
104 |
|
---|
105 | //then try request
|
---|
106 | if (!StringUtils.hasText(machineName)){
|
---|
107 | machineName = request.getParameter(MACHINE_NAME_PARAM);
|
---|
108 | debug("Machine name retrieved from HTTP Request is:" + machineName);
|
---|
109 | }
|
---|
110 | //then try the session..
|
---|
111 | if (!StringUtils.hasText(machineName)){
|
---|
112 | machineName =(String)request.getSession().getAttribute(MACHINE_NAME_PARAM);
|
---|
113 | debug("Machine name retrieved from HTTP Session is:" + machineName);
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (StringUtils.hasText(machineName)){
|
---|
117 | return machineName;
|
---|
118 | } else {
|
---|
119 | throw new UnrecognizedClientException(UnrecognizedClientException.NO_MACHINE_NAME_PARAM_MESSAGE, MACHINE_NAME_PARAM);
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | public void setServerPackageVersionDao(ServerPackageVersionDao serverPackageVersionDao) {
|
---|
124 | this.serverPackageVersionDao = serverPackageVersionDao;
|
---|
125 | }
|
---|
126 |
|
---|
127 | public ServerPackageVersionDao getServerPackageVersionDao() {
|
---|
128 | return this.serverPackageVersionDao;
|
---|
129 | }
|
---|
130 |
|
---|
131 |
|
---|
132 | }
|
---|