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