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