+++ /dev/null
-/*******************************************************************************\r
- * ============LICENSE_START====================================================\r
- * * org.onap.aaf\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- * * http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- * * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package org.onap.aaf.cadi.http;\r
-\r
-import java.net.ConnectException;\r
-import java.net.HttpURLConnection;\r
-import java.net.SocketException;\r
-import java.net.URI;\r
-import java.net.URISyntaxException;\r
-\r
-import javax.net.ssl.SSLHandshakeException;\r
-\r
-import org.onap.aaf.cadi.Access;\r
-import org.onap.aaf.cadi.CadiException;\r
-import org.onap.aaf.cadi.Locator;\r
-import org.onap.aaf.cadi.LocatorException;\r
-import org.onap.aaf.cadi.SecuritySetter;\r
-import org.onap.aaf.cadi.Access.Level;\r
-import org.onap.aaf.cadi.Locator.Item;\r
-import org.onap.aaf.cadi.client.Rcli;\r
-import org.onap.aaf.cadi.client.Retryable;\r
-\r
-import org.onap.aaf.inno.env.APIException;\r
-\r
-public class HMangr {\r
- private String apiVersion;\r
- private int readTimeout, connectionTimeout;\r
- public final Locator<URI> loc;\r
- private Access access;\r
- \r
- public HMangr(Access access, Locator<URI> loc) {\r
- readTimeout = 10000;\r
- connectionTimeout=3000;\r
- this.loc = loc;\r
- this.access = access;\r
- }\r
-\r
- /**\r
- * Reuse the same service. This is helpful for multiple calls that change service side cached data so that \r
- * there is not a speed issue.\r
- * \r
- * If the service goes down, another service will be substituted, if available.\r
- * \r
- * @param access\r
- * @param loc\r
- * @param ss\r
- * @param item\r
- * @param retryable\r
- * @return\r
- * @throws URISyntaxException \r
- * @throws Exception\r
- */\r
- public<RET> RET same(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable) throws APIException, CadiException, LocatorException {\r
- RET ret = null;\r
- boolean retry = true;\r
- int retries = 0;\r
- Rcli<HttpURLConnection> client = retryable.lastClient();\r
- try {\r
- do {\r
- // if no previous state, get the best\r
- if(retryable.item()==null) {\r
- retryable.item(loc.best());\r
- retryable.lastClient = null;\r
- }\r
- if(client==null) {\r
- Item item = retryable.item();\r
- URI uri=loc.get(item);\r
- if(uri==null) {\r
- loc.invalidate(retryable.item());\r
- if(loc.hasItems()) {\r
- retryable.item(loc.next(retryable.item()));\r
- continue;\r
- } else {\r
- throw new LocatorException("No clients available for " + loc.toString());\r
- }\r
- }\r
- client = new HRcli(this, uri,item,ss)\r
- .connectionTimeout(connectionTimeout)\r
- .readTimeout(readTimeout)\r
- .apiVersion(apiVersion);\r
- } else {\r
- client.setSecuritySetter(ss);\r
- }\r
- \r
- retry = false;\r
- try {\r
- ret = retryable.code(client);\r
- } catch (APIException | CadiException e) {\r
- Item item = retryable.item();\r
- loc.invalidate(item);\r
- retryable.item(loc.next(item));\r
- try {\r
- Throwable ec = e.getCause();\r
- if(ec instanceof java.net.ConnectException) {\r
- if(client!=null && ++retries<2) { \r
- access.log(Level.WARN,"Connection refused, trying next available service");\r
- retry = true;\r
- } else {\r
- throw new CadiException("Connection refused, no more available connections to try");\r
- }\r
- } else if(ec instanceof SSLHandshakeException) {\r
- retryable.item(null);\r
- throw e;\r
- } else if(ec instanceof SocketException) {\r
- if("java.net.SocketException: Connection reset".equals(ec.getMessage())) {\r
- access.log(Level.ERROR, ec.getMessage(), " can mean Certificate Expiration or TLS Protocol issues");\r
- }\r
- retryable.item(null);\r
- throw e;\r
- } else {\r
- retryable.item(null);\r
- throw e;\r
- }\r
- } finally {\r
- client = null;\r
- }\r
- } catch (ConnectException e) {\r
- Item item = retryable.item();\r
- loc.invalidate(item);\r
- retryable.item(loc.next(item));\r
- }\r
- } while(retry);\r
- } finally {\r
- retryable.lastClient = client;\r
- }\r
- return ret;\r
- }\r
- \r
- \r
- public<RET> RET best(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable) throws LocatorException, CadiException, APIException {\r
- if(loc==null) {\r
- throw new LocatorException("No Locator Configured");\r
- }\r
- retryable.item(loc.best());\r
- return same(ss,retryable);\r
- }\r
- public<RET> RET all(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable) throws LocatorException, CadiException, APIException {\r
- return oneOf(ss,retryable,true,null);\r
- }\r
-\r
- public<RET> RET all(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable,boolean notify) throws LocatorException, CadiException, APIException {\r
- return oneOf(ss,retryable,notify,null);\r
- }\r
- \r
- public<RET> RET oneOf(SecuritySetter<HttpURLConnection> ss, Retryable<RET> retryable,boolean notify,String host) throws LocatorException, CadiException, APIException {\r
- RET ret = null;\r
- // make sure we have all current references:\r
- loc.refresh();\r
- for(Item li=loc.first();li!=null;li=loc.next(li)) {\r
- URI uri=loc.get(li);\r
- if(host!=null && !host.equals(uri.getHost())) {\r
- break;\r
- }\r
- try {\r
- ret = retryable.code(new HRcli(this,uri,li,ss));\r
- access.log(Level.DEBUG,"Success calling",uri,"during call to all services");\r
- } catch (APIException | CadiException e) {\r
- Throwable t = e.getCause();\r
- if(t!=null && t instanceof ConnectException) {\r
- loc.invalidate(li);\r
- access.log(Level.ERROR,"Connection to",uri,"refused during call to all services");\r
- } else if(t instanceof SSLHandshakeException) {\r
- access.log(Level.ERROR,t.getMessage());\r
- loc.invalidate(li);\r
- } else if(t instanceof SocketException) {\r
- if("java.net.SocketException: Connection reset".equals(t.getMessage())) {\r
- access.log(Level.ERROR, t.getMessage(), " can mean Certificate Expiration or TLS Protocol issues");\r
- }\r
- retryable.item(null);\r
- throw e;\r
- } else {\r
- throw e;\r
- }\r
- } catch (ConnectException e) {\r
- loc.invalidate(li);\r
- access.log(Level.ERROR,"Connection to",uri,"refused during call to all services");\r
- }\r
- }\r
- \r
- if(ret == null && notify) \r
- throw new LocatorException("No available clients to call");\r
- return ret;\r
- }\r
- \r
-\r
- public void close() {\r
- // TODO Anything here?\r
- }\r
-\r
- public HMangr readTimeout(int timeout) {\r
- this.readTimeout = timeout;\r
- return this;\r
- }\r
-\r
- public int readTimeout() {\r
- return readTimeout;\r
- }\r
- \r
- public void connectionTimeout(int t) {\r
- connectionTimeout = t;\r
- }\r
-\r
- public int connectionTimout() {\r
- return connectionTimeout;\r
- }\r
-\r
- public HMangr apiVersion(String version) {\r
- apiVersion = version;\r
- return this;\r
- }\r
-\r
- public String apiVersion() {\r
- return apiVersion;\r
- }\r
-\r
-}\r