2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
19 import {Injectable} from '@angular/core';
20 import {HttpClient, HttpErrorResponse, HttpResponse} from "@angular/common/http";
21 import {environment} from "../../environments/environment";
22 import {Observable} from "rxjs";
23 import {ErrorMessage} from "../model/error-message";
24 import {TranslateService} from "@ngx-translate/core";
25 import {ErrorResult} from "../model/error-result";
30 export class ArchivaRequestService {
32 // Stores the access token locally
35 constructor(private http: HttpClient, private translator: TranslateService) {
39 * Executes a rest call to the archiva / redback REST services.
40 * @param type the type of the call (get, post, update)
41 * @param module the module (archiva, redback)
42 * @param service the REST service to call
43 * @param input the input data, if this is a POST or UPDATE request
45 executeRestCall<R>(type: string, module: string, service: string, input: object): Observable<R> {
46 let httpArgs = this.getHttpOptions(type, module, service, input);
47 httpArgs['options']['observe'] = 'body';
48 httpArgs['options']['responseType'] = 'json';
50 let lType = type.toLowerCase();
52 return this.http.get<R>(httpArgs.url, httpArgs.options);
53 } else if (lType == "head" ) {
54 return this.http.head<R>(httpArgs.url, httpArgs.options);
55 } else if (lType == "post") {
56 return this.http.post<R>(httpArgs.url, input, httpArgs.options);
57 } else if (lType == "delete") {
58 return this.http.delete<R>(httpArgs.url, httpArgs.options);
59 } else if (lType == "put") {
60 return this.http.put<R>(httpArgs.url, input, httpArgs.options);
64 private getHttpOptions(type: string, module: string, service: string, input: object) {
65 let modulePath = environment.application.servicePaths[module];
66 let url = environment.application.baseUrl + environment.application.restPath + "/" + modulePath + "/" + service;
67 let token = this.getToken();
71 "Authorization": "Bearer " + token
76 let options = {'headers': headers}
77 if (type.toLowerCase()=='get') {
82 options['params'] = params;
84 return {'url':url, 'options':options}
87 executeResponseCall<R>(type: string, module: string, service:string, input:object) : Observable<HttpResponse<R>> {
88 let httpArgs = this.getHttpOptions(type, module, service, input);
89 httpArgs['options']['observe'] = 'response';
90 httpArgs['options']['responseType'] = 'json';
91 let lType = type.toLowerCase();
93 return this.http.get<HttpResponse<R>>(httpArgs.url, httpArgs.options);
94 } else if (lType=='head') {
95 return this.http.head<HttpResponse<R>>(httpArgs.url, httpArgs.options);
96 } else if (lType == 'post') {
97 return this.http.post<HttpResponse<R>>(httpArgs.url, input, httpArgs.options);
98 } else if (lType=='delete') {
99 return this.http.delete<HttpResponse<R>>(httpArgs.url, httpArgs.options);
100 } else if (lType=='put') {
101 return this.http.put<HttpResponse<R>>(httpArgs.url, input, httpArgs.options);
105 public resetToken() {
106 this.accessToken = null;
109 private getToken(): string {
110 if (this.accessToken != null) {
111 return this.accessToken;
113 let token = localStorage.getItem("access_token");
114 if (token != null && token != "") {
115 this.accessToken = token;
124 * Translates a given error message to the current set language.
125 * @param errorMsg the errorMsg as returned by a REST call
127 public translateError(errorMsg: ErrorMessage): string {
128 if (errorMsg.error_key != null && errorMsg.error_key != '') {
130 if (errorMsg.args != null && errorMsg.args.length > 0) {
131 for (let i = 0; i < errorMsg.args.length; i++) {
132 parms['arg' + i] = errorMsg.args[i];
135 return this.translator.instant('api.' + errorMsg.error_key, parms);
139 public getTranslatedErrorResult(httpError : HttpErrorResponse) : ErrorResult {
140 let errorResult = httpError.error as ErrorResult;
141 errorResult.status = httpError.status;
142 if (errorResult.error_messages!=null) {
143 for (let message of errorResult.error_messages) {
144 if (message.message==null || message.message=='') {
145 message.message = this.translateError(message);