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;
37 * Oclip Auth client helps to do login and logout.
40 public class OnapCommandHttpAuthClient {
42 private OnapHttpCommand cmd = null;
44 private OnapHttpConnection http = null;
46 private Map<String, String> loginCache = new HashMap<>();
48 public OnapCommandHttpAuthClient(OnapHttpCommand cmd) {
50 this.http = new OnapHttpConnection();
56 * @throws OnapCommandException
59 public void login() throws OnapCommandException {
61 // For development purpose, its introduced and is not supported for production
62 if (Boolean.parseBoolean(OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.OPEN_IGNORE_AUTH))) {
66 OnapCommand login = OnapCommandSchemaHttpLoader.findAuthCommand(this.cmd, "login");
68 OnapCommandUtils.copyParamsFrom(this.cmd, login);
71 //It is safely assumed that all outputs are considered as common http headers.
72 for (OnapCommandResultAttribute attr: login.getResult().getRecords()) {
73 String headerValue = attr.getValues().get(0);
74 if (headerValue != null && !headerValue.isEmpty()) {
75 this.loginCache.put(attr.getName(), attr.getValues().get(0));
79 this.http.setCommonHeaders(this.loginCache);
85 * @throws OnapCommandException
88 public void logout() throws OnapCommandException {
89 // For development purpose, its introduced and is not supported for production
90 if (Boolean.parseBoolean(OnapCommandConfig.getPropertyValue(OnapCommandHttpConstants.OPEN_IGNORE_AUTH))) {
94 OnapCommand logout = OnapCommandSchemaHttpLoader.findAuthCommand(this.cmd, "logout");
96 //for logout, share the login cache, which would be used for logout such a token
97 OnapCommandUtils.copyParamsFrom(this.cmd, logout, this.loginCache);
105 * Find given service base path.
107 * @throws OnapCommandException
110 public String getServiceUrl() throws OnapCommandException {
111 return this.getServiceUrl(this.cmd);
114 private String getServiceUrl(OnapHttpCommand cmd) throws OnapCommandException {
115 if (cmd.getService().isModeDirect() ||
116 Boolean.parseBoolean(cmd.getParametersMap().get(OnapCommandHttpConstants.DEFAULT_PARAMETER_NO_CATALOG).getValue().toString())){
117 return cmd.getParametersMap().get(OnapCommandHttpConstants.DEAFULT_PARAMETER_HOST_URL).getValue().toString();
118 } else { //Catalog mode
119 OnapCommand catalog = OnapCommandRegistrar.getRegistrar().get("catalog", cmd.getInfo().getProduct());
121 Map<String, String> paramsOverrides = new HashMap<>();
122 paramsOverrides.put(OnapCommandHttpConstants.CATALOG_SERVICE_NAME, cmd.getService().getName());
123 paramsOverrides.put(OnapCommandHttpConstants.CATALOG_SERVICE_VERSION, cmd.getService().getVersion());
125 OnapCommandUtils.copyParamsFrom(cmd, catalog, paramsOverrides);
129 String hostUrl = catalog.getResult().getRecordsMap().get(OnapCommandHttpConstants.CATALOG_SERVICE_HOST_URL).getValues().get(0);
130 hostUrl = hostUrl.trim();
131 if (hostUrl.endsWith("/")) {
132 hostUrl = hostUrl.substring(0, hostUrl.length()-1);
135 String basePath = catalog.getResult().getRecordsMap().get(OnapCommandHttpConstants.CATALOG_SERVICE_BASE_PATH).getValues().get(0);
136 basePath = basePath.trim();
137 if (basePath.startsWith("/")) {
138 basePath = basePath.substring(1);
141 return hostUrl + "/" + basePath;
146 * Http call to external service.
150 * @return http result
151 * @throws OnapCommandHttpFailure
154 public HttpResult run(HttpInput input) throws OnapCommandHttpFailure {
155 return this.http.request(input);