Project

General

Profile

1
import { Injectable, Inject, PLATFORM_ID } from '@angular/core';
2
import { HttpInterceptor, HttpRequest, HttpHandler } from '@angular/common/http';
3
import { TransferState } from '@angular/platform-browser';
4
import { properties } from "../../environments/environment";
5

    
6
@Injectable({
7
  providedIn: 'root'
8
})
9
export class CacheInterceptorService implements HttpInterceptor {
10
  cachingRequests = [
11
    // Remove 2 following lines when bug with hidden refine filters is solved.
12
    "/resources2/?format=json&refine=true&fields=resultbestaccessright&fields=relfunder&fields=relfundinglevel0_id&fields=relproject&fields=instancetypename&fields=resultlanguagename&fields=community&fields=resulthostingdatasource&fields=country&&fq=resultbestaccessright exact \"Open Access\"&type=results&page=0&size=0",
13
    "/resources2/?format=json&refine=true&fields=funder&fields=fundinglevel0_id&fields=projectstartyear&fields=projectendyear&fields=projectecsc39&&type=projects&page=0&size=0",
14

    
15
    "/resources2/?format=json&refine=true&fields=resultbestaccessright&fields=relfunder&fields=instancetypename&fields=resultlanguagename&fields=community&fields=resulthostingdatasource&fields=country&&type=results&page=0&size=0",
16
    "/resources2/?format=json&refine=true&fields=resultbestaccessright&fields=relfunder&fields=relfundinglevel0_id&fields=relproject&fields=instancetypename&fields=resultlanguagename&fields=community&fields=resulthostingdatasource&fields=country&&fq=resultbestaccessright%20exact%20%22Open%20Access%22&fq=relfunder%20exact%20%22ec__________%3A%3AEC%7C%7CEuropean%20Commission%7C%7CEC%22&type=results&page=0&size=0",
17

    
18
    "/resources2/?format=json&refine=true&fields=resultbestaccessright&fields=relfunder&fields=instancetypename&fields=resultlanguagename&fields=community&fields=resulthostingdatasource&fields=country&&fq=resultbestaccessright%20exact%20%22Open%20Access%22&type=results&page=0&size=0",
19
    "/resources2/?format=json&refine=true&fields=resultbestaccessright&fields=relfunder&fields=instancetypename&fields=resultlanguagename&fields=community&fields=resulthostingdatasource&fields=country&&fq=resultbestaccessright exact \"Open Access\"&type=results&page=0&size=0",
20
    "/resources2/?format=json&refine=true&fields=funder&fields=projectstartyear&fields=projectendyear&fields=projectecsc39&&type=projects&page=0&size=0",
21
    "/resources2/?format=json&refine=true&fields=datasourcetypeuiname&fields=datasourceodlanguages&fields=datasourceodcontenttypes&fields=datasourcecompatibilityname&fields=country&fields=collectedfromname&&type=datasources&page=0&size=0",
22
    "/resources2/?format=json&query= ( (reldatasourcecompatibilityid exact driver or reldatasourcecompatibilityid exact driver-openaire2.0 or reldatasourcecompatibilityid exact openaire2.0 or reldatasourcecompatibilityid exact openaire3.0 or reldatasourcecompatibilityid exact openaire4.0 or reldatasourcecompatibilityid exact openaire-cris_1.1 or reldatasourcecompatibilityid exact openaire2.0_data or reldatasourcecompatibilityid exact hostedBy or relproject=*) ) &refine=true&fields=country&&type=organizations&page=0&size=0",
23

    
24
    "/projects/?format=json&refine=true&page=1&size=0&fields=funder",
25

    
26
    "/resources/?format=json&query=( oaftype exact datasource  )&refine=true&fields=datasourcetypeuiname&fields=country&fields=datasourceodsubjects&fields=datasourceodcontenttypes&fields=datasourcecompatibilityname&&type=datasources&page=0&size=0",
27

    
28
    "/search/v2/api/datasources/count?format=json", "/search/v2/api/publications/count?format=json", "/search/v2/api/datasets/count?format=json", "/search/v2/api/software/count?format=json", "/search/v2/api/other/count?format=json",
29
    "relresulttype%3Dpublication"
30
  ];
31

    
32

    
33
  constructor(private transferState: TransferState, @Inject(PLATFORM_ID) private platformId: any) {}
34

    
35
  public intercept(request: HttpRequest<any>, next: HttpHandler): any {
36
    // console.debug(new Date());
37

    
38
    if(this.checkForCachedRequests(request.url)) {
39
      const dupReq = request.clone({url: properties.cacheUrl + encodeURIComponent(request.url)});
40
      return next.handle(dupReq);
41
    } else {
42
      return next.handle(request);
43
    }
44
  }
45

    
46
  public checkForCachedRequests(url){
47
    if(url.indexOf("refine=true") !== -1 || url.indexOf("/count?format=json") !== -1 || url.indexOf("relresulttype%3Dpublication") !== -1) {
48
      return this.cachingRequests.some(partUrl => (url.indexOf(partUrl) !== -1));
49
    }
50
    return false;
51
  }
52

    
53
}
(1-1/4)