Clears user info when changing service URL
[mkjsf-moved-to-github.git] / src / main / java / com / indexdata / mkjsf / pazpar2 / Pz2ProxyBean.java
1 package com.indexdata.mkjsf.pazpar2;\r
2 \r
3 import java.io.IOException;\r
4 import java.io.UnsupportedEncodingException;\r
5 \r
6 import javax.annotation.PostConstruct;\r
7 import javax.enterprise.context.SessionScoped;\r
8 import javax.enterprise.inject.Alternative;\r
9 import javax.inject.Inject;\r
10 import javax.inject.Named;\r
11 \r
12 import org.apache.log4j.Logger;\r
13 \r
14 import com.indexdata.mkjsf.config.ConfigurationReader;\r
15 import com.indexdata.mkjsf.pazpar2.sp.ServiceProxyClient;\r
16 import com.indexdata.mkjsf.pazpar2.sp.ServiceProxyInterface;\r
17 import com.indexdata.mkjsf.pazpar2.sp.auth.ServiceProxyUser;\r
18 import com.indexdata.mkjsf.utils.Utils;\r
19 \r
20 @Named("pz2") @SessionScoped @Alternative\r
21 public class Pz2ProxyBean extends Pz2Bean implements ServiceProxyInterface {\r
22     \r
23   private static final long serialVersionUID = 4221824985678758225L;\r
24   private static Logger logger = Logger.getLogger(Pz2ProxyBean.class);  \r
25   private String initDocFileName = "";\r
26   private String initDocResponse = "";\r
27     \r
28   @Inject ConfigurationReader configurator;\r
29   @Inject ServiceProxyUser user;    \r
30   \r
31   public Pz2ProxyBean() {\r
32   }\r
33   \r
34   @PostConstruct\r
35   public void postConstruct() {\r
36     if (searchClient == null) {\r
37       logger.debug(Utils.objectId(this) + " will instantiate a ServiceProxyClient next.");    \r
38       searchClient = new ServiceProxyClient();\r
39       logger.info("Using [" + Utils.objectId(searchClient) + "] configured by [" \r
40                             + Utils.objectId(configurator) + "]" );    \r
41       configureClient(searchClient,configurator);\r
42       stateMgr.addStateListener(this);      \r
43     } else {\r
44       logger.debug("Pz2ProxyBean:postConstruct: searchClient already instantiated " +\r
45                         "during construction of parent object Pz2Bean.");\r
46     }\r
47   }\r
48   \r
49   public void login(String un, String pw) {\r
50     if (user.isAuthenticated() && user.getName().equals(un) && ((ServiceProxyClient) searchClient).checkAuthentication(user)) {\r
51       logger.info("Repeat request from UI to authenticate user. Auth verified for given user name so skipping log-in.");\r
52     } else {\r
53       logger.info("doing un/pw login");\r
54       user.setName(un);\r
55       user.setPassword(pw);\r
56       login("dummy");\r
57     }\r
58   }\r
59 \r
60   @Override\r
61   public String login(String navigateTo) {\r
62     logger.info("doing login");\r
63     ((ServiceProxyClient)searchClient).authenticate(user);    \r
64     pzreq.getRecord().removeParametersInState();\r
65     pzreq.getSearch().removeParametersInState();\r
66     pzresp.reset();\r
67     return navigateTo;\r
68   }\r
69   \r
70   public void ipAuthenticate (ServiceProxyUser user) {\r
71     if (!user.isAuthenticated()) {\r
72       ((ServiceProxyClient)searchClient).ipAuthenticate(user);\r
73     }\r
74   }\r
75 \r
76   @Override\r
77   public void setServiceProxyUrl(String url) {\r
78     logger.info("Setting Service Proxy url: " + url);    \r
79     if (url!=null & !url.equals(((ServiceProxyClient)searchClient).getServiceProxyUrl())) {\r
80       pzreq.getRecord().removeParametersInState();\r
81       pzreq.getSearch().removeParametersInState();\r
82       pzresp.reset();\r
83       user.clear();\r
84       ((ServiceProxyClient)searchClient).setServiceProxyUrl(url);\r
85     }    \r
86   }\r
87   \r
88   public String getServiceProxyUrl() {\r
89     return ((ServiceProxyClient)searchClient).getServiceProxyUrl();\r
90   }\r
91     \r
92   public String getInitDocPath () {\r
93     return searchClient.getConfiguration().get("INIT_DOC_PATH");\r
94   }\r
95   \r
96   @Override\r
97   public void setInitFileName(String fileName) {\r
98     this.initDocFileName = fileName;\r
99     \r
100   }\r
101 \r
102   @Override\r
103   public String getInitFileName() {\r
104     return initDocFileName;\r
105   }\r
106 \r
107   @Override\r
108   public String postInit() throws UnsupportedEncodingException, IOException {    \r
109     String initDocPath = ((ServiceProxyClient)searchClient).getInitDocPaths()[0];\r
110     logger.info("Paths: " + ((ServiceProxyClient)searchClient).getInitDocPaths());\r
111     logger.info("Path: " + initDocPath);\r
112     pzresp.reset();\r
113     byte[] response = ((ServiceProxyClient)searchClient).postInitDoc(initDocPath + getInitFileName());\r
114     initDocResponse = new String(response,"UTF-8");\r
115     return initDocResponse;\r
116   }\r
117   \r
118   @Override\r
119   public String postInit(byte[] initDoc) throws UnsupportedEncodingException, IOException {    \r
120     pzresp.reset();\r
121     byte[] response = ((ServiceProxyClient)searchClient).postInitDoc(initDoc);\r
122     initDocResponse = new String(response,"UTF-8");\r
123     return initDocResponse;\r
124   }\r
125 \r
126 \r
127   @Override\r
128   public String getInitResponse() {\r
129     return initDocResponse;\r
130   }\r
131   \r
132 }\r