2 * Copyright 2017 Huawei Technologies Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.cli.fw.http.auth;
19 import java.util.HashMap;
22 import org.onap.cli.fw.cmd.OnapCommand;
23 import org.onap.cli.fw.conf.OnapCommandConfig;
24 import org.onap.cli.fw.error.OnapCommandException;
25 import org.onap.cli.fw.http.cmd.OnapHttpCommand;
26 import org.onap.cli.fw.http.conf.OnapCommandHttpConstants;
27 import org.onap.cli.fw.http.connect.HttpInput;
28 import org.onap.cli.fw.http.connect.HttpResult;
29 import org.onap.cli.fw.http.connect.OnapHttpConnection;
30 import org.onap.cli.fw.http.error.OnapCommandHttpFailure;
31 import org.onap.cli.fw.http.schema.OnapCommandSchemaHttpLoader;
32 import org.onap.cli.fw.output.OnapCommandResultAttribute;
33 import org.onap.cli.fw.registrar.OnapCommandRegistrar;
34 import org.onap.cli.fw.utils.OnapCommandUtils;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import java.io.IOException;
38 import org.onap.cli.fw.http.error.OnapCommandLoginFailed;
41 * Oclip Auth client helps to do login and logout.
44 public class OnapCommandHttpAuthClient {
46 private static Logger logger = LoggerFactory.getLogger(OnapCommandHttpAuthClient.class); //NOSONAR
47 private OnapHttpCommand cmd = null;
49 private OnapHttpConnection http = null;
51 private Map<String, String> loginCache = new HashMap<>();
53 public OnapCommandHttpAuthClient(OnapHttpCommand cmd) {
55 this.http = new OnapHttpConnection();
61 * @throws OnapCommandException
64 public void login() throws OnapCommandException {
66 // For development purpose, its introduced and is not supported for production
67 if (Boolean.parseBoolean(OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.OPEN_IGNORE_AUTH))) {
71 OnapCommand login = OnapCommandSchemaHttpLoader.findAuthCommand(this.cmd, "login");
73 OnapCommandUtils.copyParamsFrom(this.cmd, login);
76 //It is safely assumed that all outputs are considered as common http headers.
77 for (OnapCommandResultAttribute attr: login.getResult().getRecords()) {
78 String headerValue = attr.getValues().get(0);
79 if (headerValue != null && !headerValue.isEmpty()) {
80 this.loginCache.put(attr.getName(), attr.getValues().get(0));
84 this.http.setCommonHeaders(this.loginCache);
90 * @throws OnapCommandException
93 public void logout() throws OnapCommandException {
94 // For development purpose, its introduced and is not supported for production
95 if (Boolean.parseBoolean(OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.OPEN_IGNORE_AUTH))) {
99 OnapCommand logout = OnapCommandSchemaHttpLoader.findAuthCommand(this.cmd, "logout");
101 //for logout, share the login cache, which would be used for logout such a token
102 OnapCommandUtils.copyParamsFrom(this.cmd, logout, this.loginCache);
108 } catch (IOException e) {
109 throw new OnapCommandLoginFailed("Exception when closing httpclient");
114 * Find given service base path.
116 * @throws OnapCommandException
119 public String getServiceUrl() throws OnapCommandException {
120 return this.getServiceUrl(this.cmd);
123 private String getServiceUrl(OnapHttpCommand cmd) throws OnapCommandException {
124 if (cmd.getService().isModeDirect() ||
125 Boolean.parseBoolean(cmd.getParametersMap().get(OnapCommandHttpConstants.DEFAULT_PARAMETER_NO_CATALOG).getValue().toString())){
126 return cmd.getParametersMap().get(OnapCommandHttpConstants.DEAFULT_PARAMETER_HOST_URL).getValue().toString();
127 } else { //Catalog mode
128 OnapCommand catalog = OnapCommandRegistrar.getRegistrar().get("catalog", cmd.getInfo().getProduct());
130 Map<String, String> paramsOverrides = new HashMap<>();
131 paramsOverrides.put(OnapCommandHttpConstants.CATALOG_SERVICE_NAME, cmd.getService().getName());
132 paramsOverrides.put(OnapCommandHttpConstants.CATALOG_SERVICE_VERSION, cmd.getService().getVersion());
134 OnapCommandUtils.copyParamsFrom(cmd, catalog, paramsOverrides);
138 String hostUrl = catalog.getResult().getRecordsMap().get(OnapCommandHttpConstants.CATALOG_SERVICE_HOST_URL).getValues().get(0);
139 hostUrl = hostUrl.trim();
140 if (hostUrl.endsWith("/")) {
141 hostUrl = hostUrl.substring(0, hostUrl.length()-1);
144 String basePath = catalog.getResult().getRecordsMap().get(OnapCommandHttpConstants.CATALOG_SERVICE_BASE_PATH).getValues().get(0);
145 basePath = basePath.trim();
146 if (basePath.startsWith("/")) {
147 basePath = basePath.substring(1);
150 return hostUrl + "/" + basePath;
155 * Http call to external service.
159 * @return http result
160 * @throws OnapCommandHttpFailure
163 public HttpResult run(HttpInput input) throws OnapCommandHttpFailure {
164 return this.http.request(input);