diff --git a/dmp-admin/src/app/services/interceptor.ts b/dmp-admin/src/app/services/interceptor.ts index 8bf83e057..1fad30ff7 100644 --- a/dmp-admin/src/app/services/interceptor.ts +++ b/dmp-admin/src/app/services/interceptor.ts @@ -19,7 +19,6 @@ export class GlobalInterceptor implements HttpInterceptor { }, (err: any) => { if (err instanceof HttpErrorResponse) { if (err.status === 401) { - console.log("logging out"); this.tokenService.logout(); } } diff --git a/dmp-admin/src/app/services/rest-base.ts b/dmp-admin/src/app/services/rest-base.ts index 2a056841a..4fdf398f2 100644 --- a/dmp-admin/src/app/services/rest-base.ts +++ b/dmp-admin/src/app/services/rest-base.ts @@ -35,7 +35,7 @@ export class RestBase { loginPath : string = this.protocol+"://"+this.hostname+":"+this.port+"/"+this.webappname+"/login/"; - restBasePath: string = this.protocol+"://"+this.hostname+":"+this.port+"/"+this.webappname+"/"+this.restpath+"/"; + restPath: string = this.protocol+"://"+this.hostname+":"+this.port+"/"+this.webappname+"/"+this.restpath+"/"; public login(path : string, data : any){ @@ -45,13 +45,13 @@ export class RestBase { public get(path : string, params? : any){ var options = this.createOptions(params); - return this.http.get(this.restBasePath + path, options); + return this.http.get(this.restPath + path, options); } public post(path : string, data : any, params? : any){ var options = this.createOptions(params); - return this.http.post(this.restBasePath + path, JSON.stringify(data), options); + return this.http.post(this.restPath + path, JSON.stringify(data), options); } diff --git a/dmp-frontend/src/app/app.module.ts b/dmp-frontend/src/app/app.module.ts index 3fe832bb4..34b42b172 100644 --- a/dmp-frontend/src/app/app.module.ts +++ b/dmp-frontend/src/app/app.module.ts @@ -24,6 +24,7 @@ import { TocComponent } from './form/tableOfContents/toc.component'; import { ProjectsModule } from './projects/project.module'; import { PaginationService } from './services/pagination.service'; import { EestoreService } from './services/eestore.service'; +import { GlobalInterceptor } from './services/interceptor'; import { DatasetsModule } from './datasets/dataset.module'; import { DmpModule } from './dmps/dmp.module'; import { TabModule } from './tabs/tab.module'; @@ -53,7 +54,13 @@ import { TabModule } from './tabs/tab.module'; DataTableModule ], - providers: [ServerService, dataModelBuilder, AuthGuard, PaginationService, TokenService, LocalStorageService, RestBase, EestoreService], + providers: [{ + provide: HTTP_INTERCEPTORS, + useClass: GlobalInterceptor, + multi: true, + }, + ServerService, dataModelBuilder, AuthGuard, PaginationService, TokenService, LocalStorageService, RestBase, EestoreService + ], bootstrap: [AppComponent] }) export class AppModule { diff --git a/dmp-frontend/src/app/services/interceptor.ts b/dmp-frontend/src/app/services/interceptor.ts new file mode 100644 index 000000000..522b7a0fb --- /dev/null +++ b/dmp-frontend/src/app/services/interceptor.ts @@ -0,0 +1,30 @@ +import {Injectable} from '@angular/core'; +import {Observable} from 'rxjs/Observable'; +import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse, HttpErrorResponse} from '@angular/common/http'; + + +@Injectable() +export class GlobalInterceptor implements HttpInterceptor { + + constructor() { + + } + + intercept(req: HttpRequest, next: HttpHandler): Observable> { + + return next.handle(req).do((event: HttpEvent) => { + /* + if (event instanceof HttpResponse) { + console.log("response ok"); + } + */ + }, (err: any) => { + if (err instanceof HttpErrorResponse) { + if (err.status === 401) { + EDW VALE TO KWDIKA GIA TO LOGOUT + } + } + }); + + } +} \ No newline at end of file diff --git a/dmp-frontend/src/app/services/rest-base.ts b/dmp-frontend/src/app/services/rest-base.ts index 13b2e02ba..f793da4ee 100644 --- a/dmp-frontend/src/app/services/rest-base.ts +++ b/dmp-frontend/src/app/services/rest-base.ts @@ -31,8 +31,8 @@ export class RestBase { restPath: string = this.protocol+"://"+this.hostname+":"+this.port+"/"+this.webappname+"/rest/"; - public proxy_get(path : string){ - var options = this.createOptions(); + public proxy_get(path : string, params? : any){ + var options = this.createOptions(params); return this.http.get(this.proxyPath +"proxy?url="+ path, options); } @@ -41,30 +41,29 @@ export class RestBase { return this.http.post(this.loginPath + path, JSON.stringify(data), options); } - public get(path : string){ - var options = this.createOptions(); + public get(path : string, params? : any){ + var options = this.createOptions(params); return this.http.get(this.restPath + path, options); } - public post(path : string, data : any) { - var options = this.createOptions(); + public post(path : string, data : any, params? : any){ + var options = this.createOptions(params); return this.http.post(this.restPath + path, JSON.stringify(data), options); } - public postWithParams(path : string, data : any, params: HttpParams) { - var options = this.createOptions(); - options.params = params; - return this.http.post(this.restPath + path, JSON.stringify(data), options); - } - - - private createOptions(){ + private createOptions(parameters : any){ var token = this.tokenService.getToken(); var provider: TokenProvider = this.tokenService.getProvider(); - //var csrfToken : string = this.tokenService.getCSRFToken(); - const params = new HttpParams(); + var params = new HttpParams(); + + if(parameters != null){ + Object.entries(parameters).forEach( entry => { + params = params.set(entry[0], entry[1]); + }); + } + var headers; if(provider == TokenProvider.google) headers = new HttpHeaders().set('Content-Type', 'application/json').set("google-token", token); diff --git a/dmp-frontend/src/app/services/server.service.ts b/dmp-frontend/src/app/services/server.service.ts index ea9a3f94a..42c8d8d41 100644 --- a/dmp-frontend/src/app/services/server.service.ts +++ b/dmp-frontend/src/app/services/server.service.ts @@ -106,9 +106,10 @@ export class ServerService { public createDmpForProject2(data:any, projectid: string){ //return this.restBase.post("dmp/createforproject", data); - return this.restBase.postWithParams('dmp/create', data, - new HttpParams().set('id', projectid) - ) + return this.restBase.post('dmp/create', data, + {"id":projectid} + //new HttpParams().set('id', projectid) + ) } public createDmpForProject(data:any){