1 package com.indexdata.pz2utils4jsf.pazpar2.sp;
\r
3 import static com.indexdata.pz2utils4jsf.utils.Utils.nl;
\r
5 import java.io.ByteArrayOutputStream;
\r
7 import java.io.IOException;
\r
8 import java.nio.file.Path;
\r
9 import java.nio.file.Paths;
\r
10 import java.util.ArrayList;
\r
11 import java.util.HashMap;
\r
12 import java.util.List;
\r
13 import java.util.Map;
\r
15 import javax.enterprise.context.SessionScoped;
\r
16 import javax.inject.Named;
\r
18 import org.apache.http.HttpEntity;
\r
19 import org.apache.http.HttpResponse;
\r
20 import org.apache.http.StatusLine;
\r
21 import org.apache.http.client.ClientProtocolException;
\r
22 import org.apache.http.client.HttpClient;
\r
23 import org.apache.http.client.ResponseHandler;
\r
24 import org.apache.http.client.methods.HttpGet;
\r
25 import org.apache.http.client.methods.HttpPost;
\r
26 import org.apache.http.conn.ClientConnectionManager;
\r
27 import org.apache.http.conn.scheme.PlainSocketFactory;
\r
28 import org.apache.http.conn.scheme.Scheme;
\r
29 import org.apache.http.conn.scheme.SchemeRegistry;
\r
30 import org.apache.http.entity.ByteArrayEntity;
\r
31 import org.apache.http.entity.FileEntity;
\r
32 import org.apache.http.impl.client.DefaultHttpClient;
\r
33 import org.apache.http.impl.conn.PoolingClientConnectionManager;
\r
34 import org.apache.http.util.EntityUtils;
\r
35 import org.apache.log4j.Logger;
\r
37 import com.indexdata.masterkey.config.MissingMandatoryParameterException;
\r
38 import com.indexdata.masterkey.pazpar2.client.exceptions.Pazpar2ErrorException;
\r
39 import com.indexdata.pz2utils4jsf.config.Configuration;
\r
40 import com.indexdata.pz2utils4jsf.config.ConfigurationReader;
\r
41 import com.indexdata.pz2utils4jsf.errors.ConfigurationException;
\r
42 import com.indexdata.pz2utils4jsf.pazpar2.CommandParameter;
\r
43 import com.indexdata.pz2utils4jsf.pazpar2.CommandResponse;
\r
44 import com.indexdata.pz2utils4jsf.pazpar2.Pazpar2Command;
\r
45 import com.indexdata.pz2utils4jsf.pazpar2.SearchClient;
\r
46 import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.AuthenticationEntity;
\r
47 import com.indexdata.pz2utils4jsf.pazpar2.sp.auth.ServiceProxyUser;
\r
48 import com.indexdata.pz2utils4jsf.utils.Utils;
\r
50 @Named @SessionScoped
\r
51 public class ServiceProxyClient implements SearchClient {
\r
53 private static final long serialVersionUID = -4031644009579840277L;
\r
54 private static Logger logger = Logger.getLogger(ServiceProxyClient.class);
\r
55 public static final String MODULENAME = "proxyclient";
\r
56 public static final String SERVICE_PROXY_URL = "SERVICE_PROXY_URL";
\r
57 public static final String SP_INIT_DOC_PATHS = "SP_INIT_DOC_PATHS";
\r
58 private String serviceUrl = "undefined";
\r
59 private String[] initDocPaths = null;
\r
60 private Configuration config = null;
\r
62 ProxyPz2ResponseHandler handler = new ProxyPz2ResponseHandler();
\r
63 private HttpClient client;
\r
64 private ServiceProxyUser user;
\r
66 public ServiceProxyClient () {
\r
67 SchemeRegistry schemeRegistry = new SchemeRegistry();
\r
68 schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
\r
69 ClientConnectionManager cm = new PoolingClientConnectionManager(schemeRegistry);
\r
70 client = new DefaultHttpClient(cm);
\r
74 public void configure (ConfigurationReader configReader) {
\r
75 logger.info(Utils.objectId(this) + " is configuring using the provided " + Utils.objectId(configReader));
\r
77 config = configReader.getConfiguration(this);
\r
78 serviceUrl = config.getMandatory(SERVICE_PROXY_URL);
\r
79 this.initDocPaths = getMultiProperty(config.get(SP_INIT_DOC_PATHS));
\r
80 } catch (ConfigurationException c) {
\r
81 c.printStackTrace();
\r
82 } catch (MissingMandatoryParameterException mmp) {
\r
83 mmp.printStackTrace();
\r
87 private String[] getMultiProperty(String prop) {
\r
89 return prop.split(",");
\r
95 public boolean authenticate (AuthenticationEntity user) {
\r
97 logger.info("Authenticating [" + user.getProperty("name") + "]");
\r
98 this.user = (ServiceProxyUser) user;
\r
99 Pazpar2Command auth = new Pazpar2Command("auth");
\r
100 auth.setParameter(new CommandParameter("action","=","login"));
\r
101 auth.setParameter(new CommandParameter("username","=",user.getProperty("name")));
\r
102 auth.setParameter(new CommandParameter("password","=",user.getProperty("password")));
\r
103 byte[] response = send(auth);
\r
104 String responseStr = new String(response,"UTF-8");
\r
105 logger.info(responseStr);
\r
106 if (responseStr.contains("FAIL")) {
\r
111 } catch (ClientProtocolException e) {
\r
112 // TODO Auto-generated catch block
\r
113 e.printStackTrace();
\r
115 } catch (IOException e) {
\r
116 // TODO Auto-generated catch block
\r
117 e.printStackTrace();
\r
122 public boolean checkAuthentication () {
\r
124 Pazpar2Command check = new Pazpar2Command("auth");
\r
125 check.setParameter(new CommandParameter("action","=","check"));
\r
126 byte[] response = send(check);
\r
127 logger.info(new String(response,"UTF-8"));
\r
128 } catch (ClientProtocolException e) {
\r
129 // TODO Auto-generated catch block
\r
130 e.printStackTrace();
\r
132 } catch (IOException e) {
\r
133 // TODO Auto-generated catch block
\r
134 e.printStackTrace();
\r
141 public boolean isAuthenticatingClient () {
\r
145 public boolean isAuthenticated () {
\r
146 if (user.getProperty("name") != null && user.getProperty("password") != null) {
\r
147 return checkAuthentication();
\r
154 * Makes the request
\r
156 * @return HTTP response as a String
\r
157 * @throws ClientProtocolException
\r
158 * @throws IOException
\r
160 private byte[] send(Pazpar2Command command) throws ClientProtocolException, IOException {
\r
161 String url = serviceUrl + "?" + command.getEncodedQueryString();
\r
162 logger.info("Sending request "+url);
\r
163 HttpGet httpget = new HttpGet(url);
\r
164 byte[] response = client.execute(httpget, handler);
\r
168 public class ProxyPz2ResponseHandler implements ResponseHandler<byte[]> {
\r
169 private StatusLine statusLine = null;
\r
170 public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
\r
171 byte[] resp = null;
\r
172 HttpEntity entity = response.getEntity();
\r
173 statusLine = response.getStatusLine();
\r
174 if (entity != null) {
\r
175 resp = EntityUtils.toByteArray(entity);
\r
177 EntityUtils.consume(entity);
\r
180 public int getStatusCode() {
\r
181 return statusLine.getStatusCode();
\r
183 public String getReasonPhrase() {
\r
184 return statusLine.getReasonPhrase();
\r
188 public int getStatusCode () {
\r
189 return handler.getStatusCode();
\r
192 public String getReasonPhrase() {
\r
193 return handler.getReasonPhrase();
\r
197 public void setSearchCommand(Pazpar2Command command) {
\r
198 // Do nothing, Service Proxy is handling this
\r
202 public CommandResponse executeCommand(Pazpar2Command command,
\r
203 ByteArrayOutputStream baos) throws Pazpar2ErrorException, IOException {
\r
204 byte[] response = send(command);
\r
205 baos.write(response);
\r
206 return new ServiceProxyClientCommandResponse(getStatusCode(), new String(response,"UTF-8"));
\r
209 public ServiceProxyClient cloneMe() {
\r
210 logger.debug("Cloning Pz2Client");
\r
211 ServiceProxyClient clone = new ServiceProxyClient();
\r
212 clone.client = this.client;
\r
213 clone.serviceUrl = this.serviceUrl;
\r
214 clone.initDocPaths = this.initDocPaths;
\r
219 public Map<String, String> getDefaults() {
\r
220 return new HashMap<String,String>();
\r
224 public String getModuleName() {
\r
229 public List<String> documentConfiguration () {
\r
230 List<String> doc = new ArrayList<String>();
\r
231 doc.add(nl+ MODULENAME + " was configured to access the Pazpar2 service proxy at: " + serviceUrl);
\r
235 public byte[] postInitDoc (String filePath) throws IOException {
\r
236 logger.info("Looking to post the file in : [" + filePath +"]");
\r
237 HttpPost post = new HttpPost(serviceUrl+"?command=init&includeDebug=yes");
\r
238 File initDoc = new File(filePath);
\r
239 logger.info("Posting to SP: ");
\r
240 Path path = Paths.get(filePath);
\r
242 if (logger.isDebugEnabled()) {
\r
243 try (Scanner scanner = new Scanner(path, "UTF-8")){
\r
244 while (scanner.hasNextLine()){
\r
245 System.out.println(scanner.nextLine());
\r
250 post.setEntity(new FileEntity(initDoc));
\r
251 byte[] response = client.execute(post, handler);
\r
252 logger.info("Response on POST was: " + new String(response,"UTF-8"));
\r
256 public String[] getInitDocPaths () {
\r
257 logger.info("Get init doc paths ");
\r
258 logger.info("length: " + initDocPaths.length);
\r
259 return initDocPaths;
\r
262 public byte[] postInitDoc(byte[] initDoc) throws IOException {
\r
263 HttpPost post = new HttpPost(serviceUrl+"?command=init&includeDebug=yes");
\r
264 post.setEntity(new ByteArrayEntity(initDoc));
\r
265 byte[] response = client.execute(post, handler);
\r
266 logger.info("Response on POST was: " + new String(response,"UTF-8"));
\r
270 public void setServiceProxyUrl (String url) {
\r
274 public String getServiceProxyUrl () {
\r
278 public Configuration getConfiguration () {
\r