[AAF-21] Initial code import
[aaf/cadi.git] / client / src / main / java / com / att / cadi / http / HRcli.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aai\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * Copyright © 2017 Amdocs\r
7  * * ===========================================================================\r
8  * * Licensed under the Apache License, Version 2.0 (the "License");\r
9  * * you may not use this file except in compliance with the License.\r
10  * * You may obtain a copy of the License at\r
11  * * \r
12  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  * * \r
14  *  * Unless required by applicable law or agreed to in writing, software\r
15  * * distributed under the License is distributed on an "AS IS" BASIS,\r
16  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
17  * * See the License for the specific language governing permissions and\r
18  * * limitations under the License.\r
19  * * ============LICENSE_END====================================================\r
20  * *\r
21  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
22  * *\r
23  ******************************************************************************/\r
24 package com.att.cadi.http;\r
25 \r
26 import java.net.HttpURLConnection;\r
27 import java.net.URI;\r
28 import java.net.URISyntaxException;\r
29 \r
30 import com.att.aft.dme2.api.DME2Exception;\r
31 import com.att.cadi.CadiException;\r
32 import com.att.cadi.LocatorException;\r
33 import com.att.cadi.SecuritySetter;\r
34 import com.att.cadi.Locator.Item;\r
35 import com.att.cadi.client.EClient;\r
36 import com.att.cadi.client.Rcli;\r
37 import com.att.inno.env.APIException;\r
38 import com.att.inno.env.Data.TYPE;\r
39 \r
40 /**\r
41  * DME2 Rosetta Client\r
42  * \r
43  * JAXB defined JSON or XML over DME2 middleware\r
44  * \r
45  *\r
46  * @param <T>\r
47  */\r
48 public class HRcli extends Rcli<HttpURLConnection> {\r
49         private HMangr hman;\r
50         private Item item;\r
51         private SecuritySetter<HttpURLConnection> ss;\r
52 \r
53         public HRcli(HMangr hman, Item locItem, SecuritySetter<HttpURLConnection> secSet) throws URISyntaxException, LocatorException {\r
54                 item=locItem;\r
55                 uri=hman.loc.get(locItem);\r
56                 this.hman = hman;\r
57                 ss=secSet;\r
58                 type = TYPE.JSON;\r
59                 apiVersion = hman.apiVersion();\r
60         }\r
61 \r
62         public HRcli(HMangr hman, URI uri, Item locItem, SecuritySetter<HttpURLConnection> secSet) {\r
63                 locItem=item;\r
64                 this.uri = uri;\r
65                 this.hman = hman;\r
66                 ss=secSet;\r
67                 type = TYPE.JSON;\r
68                 apiVersion = hman.apiVersion();\r
69         }\r
70 \r
71         @Override\r
72         protected HRcli clone(URI uri, SecuritySetter<HttpURLConnection> ss) {\r
73                 return new HRcli(hman,uri,item,ss);\r
74         }\r
75 \r
76 \r
77 \r
78         /**\r
79          * Note from Thaniga on 11/5.  DME2Client is not expected to be reused... need a fresh one\r
80          * on each transaction, which is expected to cover the Async aspects.\r
81          * \r
82          * @return\r
83          * @throws APIException \r
84          * @throws DME2Exception \r
85          */\r
86         protected EClient<HttpURLConnection> client() throws CadiException {\r
87                 try {\r
88                         if(uri==null) {\r
89                                 Item item = hman.loc.best();\r
90                                 if(item==null) {\r
91                                         throw new CadiException("No service available for " + hman.loc.toString());\r
92                                 }\r
93                                 uri = hman.loc.get(item);\r
94                         }\r
95                         return new HClient(ss,uri,connectionTimeout);\r
96                 } catch (Exception e) {\r
97                         throw new CadiException(e);\r
98                 }\r
99         }\r
100         \r
101         /* (non-Javadoc)\r
102          * @see com.att.cadi.client.Rcli#setSecuritySetter(com.att.cadi.SecuritySetter)\r
103          */\r
104         @Override\r
105         public void setSecuritySetter(SecuritySetter<HttpURLConnection> ss) {\r
106                 this.ss = ss;\r
107         }\r
108 \r
109         /* (non-Javadoc)\r
110          * @see com.att.cadi.client.Rcli#getSecuritySetter()\r
111          */\r
112         @Override\r
113         public SecuritySetter<HttpURLConnection> getSecuritySetter() {\r
114                 return ss;\r
115         }\r
116 \r
117         public void invalidate() throws CadiException {\r
118                 try {\r
119                         hman.loc.invalidate(item);\r
120                 } catch (Exception e) {\r
121                         throw new CadiException(e);\r
122                 }\r
123         }\r
124         \r
125         public HRcli setManager(HMangr hman) {\r
126                 this.hman = hman;\r
127                 return this;\r
128         }\r
129 \r
130         public String toString() {\r
131                 return uri.toString();\r
132         }\r
133         \r
134 }\r