Skip to content

Commit 00e0028

Browse files
authored
Merge pull request DSpace#2248 from 4Science/feature/CST-5729
Implement Signposting on angular side
2 parents 00d895b + 2fc3f2b commit 00e0028

13 files changed

Lines changed: 476 additions & 38 deletions

server.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@ import * as ejs from 'ejs';
2626
import * as compression from 'compression';
2727
import * as expressStaticGzip from 'express-static-gzip';
2828
/* eslint-enable import/no-namespace */
29-
3029
import axios from 'axios';
3130
import LRU from 'lru-cache';
3231
import isbot from 'isbot';
3332
import { createCertificate } from 'pem';
3433
import { createServer } from 'https';
3534
import { json } from 'body-parser';
3635

37-
import { existsSync, readFileSync } from 'fs';
36+
import { readFileSync } from 'fs';
3837
import { join } from 'path';
3938

4039
import { enableProdMode } from '@angular/core';
@@ -180,6 +179,15 @@ export function app() {
180179
changeOrigin: true
181180
}));
182181

182+
/**
183+
* Proxy the linksets
184+
*/
185+
router.use('/signposting**', createProxyMiddleware({
186+
target: `${environment.rest.baseUrl}`,
187+
pathRewrite: path => path.replace(environment.ui.nameSpace, '/'),
188+
changeOrigin: true
189+
}));
190+
183191
/**
184192
* Checks if the rateLimiter property is present
185193
* When it is present, the rateLimiter will be enabled. When it is undefined, the rateLimiter will be disabled.

src/app/bitstream-page/bitstream-download-page/bitstream-download-page.component.spec.ts

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import { ActivatedRoute, Router } from '@angular/router';
1111
import { getForbiddenRoute } from '../../app-routing-paths';
1212
import { TranslateModule } from '@ngx-translate/core';
1313
import { CommonModule } from '@angular/common';
14+
import { SignpostingDataService } from '../../core/data/signposting-data.service';
15+
import { ServerResponseService } from '../../core/services/server-response.service';
16+
import { PLATFORM_ID } from '@angular/core';
1417

1518
describe('BitstreamDownloadPageComponent', () => {
1619
let component: BitstreamDownloadPageComponent;
@@ -24,6 +27,20 @@ describe('BitstreamDownloadPageComponent', () => {
2427
let router;
2528

2629
let bitstream: Bitstream;
30+
let serverResponseService: jasmine.SpyObj<ServerResponseService>;
31+
let signpostingDataService: jasmine.SpyObj<SignpostingDataService>;
32+
33+
const mocklink = {
34+
href: 'http://test.org',
35+
rel: 'test',
36+
type: 'test'
37+
};
38+
39+
const mocklink2 = {
40+
href: 'http://test2.org',
41+
rel: 'test',
42+
type: 'test'
43+
};
2744

2845
function init() {
2946
authService = jasmine.createSpyObj('authService', {
@@ -44,8 +61,8 @@ describe('BitstreamDownloadPageComponent', () => {
4461
bitstream = Object.assign(new Bitstream(), {
4562
uuid: 'bitstreamUuid',
4663
_links: {
47-
content: {href: 'bitstream-content-link'},
48-
self: {href: 'bitstream-self-link'},
64+
content: { href: 'bitstream-content-link' },
65+
self: { href: 'bitstream-self-link' },
4966
}
5067
});
5168

@@ -54,23 +71,37 @@ describe('BitstreamDownloadPageComponent', () => {
5471
bitstream: createSuccessfulRemoteDataObject(
5572
bitstream
5673
)
74+
}),
75+
params: observableOf({
76+
id: 'testid'
5777
})
5878
};
5979

6080
router = jasmine.createSpyObj('router', ['navigateByUrl']);
81+
82+
serverResponseService = jasmine.createSpyObj('ServerResponseService', {
83+
setHeader: jasmine.createSpy('setHeader'),
84+
});
85+
86+
signpostingDataService = jasmine.createSpyObj('SignpostingDataService', {
87+
getLinks: observableOf([mocklink, mocklink2])
88+
});
6189
}
6290

6391
function initTestbed() {
6492
TestBed.configureTestingModule({
6593
imports: [CommonModule, TranslateModule.forRoot()],
6694
declarations: [BitstreamDownloadPageComponent],
6795
providers: [
68-
{provide: ActivatedRoute, useValue: activatedRoute},
69-
{provide: Router, useValue: router},
70-
{provide: AuthorizationDataService, useValue: authorizationService},
71-
{provide: AuthService, useValue: authService},
72-
{provide: FileService, useValue: fileService},
73-
{provide: HardRedirectService, useValue: hardRedirectService},
96+
{ provide: ActivatedRoute, useValue: activatedRoute },
97+
{ provide: Router, useValue: router },
98+
{ provide: AuthorizationDataService, useValue: authorizationService },
99+
{ provide: AuthService, useValue: authService },
100+
{ provide: FileService, useValue: fileService },
101+
{ provide: HardRedirectService, useValue: hardRedirectService },
102+
{ provide: ServerResponseService, useValue: serverResponseService },
103+
{ provide: SignpostingDataService, useValue: signpostingDataService },
104+
{ provide: PLATFORM_ID, useValue: 'server' }
74105
]
75106
})
76107
.compileComponents();
@@ -107,6 +138,9 @@ describe('BitstreamDownloadPageComponent', () => {
107138
it('should redirect to the content link', () => {
108139
expect(hardRedirectService.redirect).toHaveBeenCalledWith('bitstream-content-link');
109140
});
141+
it('should add the signposting links', () => {
142+
expect(serverResponseService.setHeader).toHaveBeenCalled();
143+
});
110144
});
111145
describe('when the user is authorized and logged in', () => {
112146
beforeEach(waitForAsync(() => {
@@ -134,7 +168,7 @@ describe('BitstreamDownloadPageComponent', () => {
134168
fixture.detectChanges();
135169
});
136170
it('should navigate to the forbidden route', () => {
137-
expect(router.navigateByUrl).toHaveBeenCalledWith(getForbiddenRoute(), {skipLocationChange: true});
171+
expect(router.navigateByUrl).toHaveBeenCalledWith(getForbiddenRoute(), { skipLocationChange: true });
138172
});
139173
});
140174
describe('when the user is not authorized and not logged in', () => {

src/app/bitstream-page/bitstream-download-page/bitstream-download-page.component.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { Component, OnInit } from '@angular/core';
1+
import { Component, Inject, OnInit, PLATFORM_ID } from '@angular/core';
22
import { filter, map, switchMap, take } from 'rxjs/operators';
33
import { ActivatedRoute, Router } from '@angular/router';
44
import { hasValue, isNotEmpty } from '../../shared/empty.util';
5-
import { getRemoteDataPayload} from '../../core/shared/operators';
5+
import { getRemoteDataPayload } from '../../core/shared/operators';
66
import { Bitstream } from '../../core/shared/bitstream.model';
77
import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service';
88
import { FeatureID } from '../../core/data/feature-authorization/feature-id';
@@ -13,8 +13,11 @@ import { HardRedirectService } from '../../core/services/hard-redirect.service';
1313
import { getForbiddenRoute } from '../../app-routing-paths';
1414
import { RemoteData } from '../../core/data/remote-data';
1515
import { redirectOn4xx } from '../../core/shared/authorized.operators';
16-
import { Location } from '@angular/common';
16+
import { isPlatformServer, Location } from '@angular/common';
1717
import { DSONameService } from '../../core/breadcrumbs/dso-name.service';
18+
import { SignpostingDataService } from '../../core/data/signposting-data.service';
19+
import { ServerResponseService } from '../../core/services/server-response.service';
20+
import { SignpostingLink } from '../../core/data/signposting-links.model';
1821

1922
@Component({
2023
selector: 'ds-bitstream-download-page',
@@ -28,7 +31,6 @@ export class BitstreamDownloadPageComponent implements OnInit {
2831
bitstream$: Observable<Bitstream>;
2932
bitstreamRD$: Observable<RemoteData<Bitstream>>;
3033

31-
3234
constructor(
3335
private route: ActivatedRoute,
3436
protected router: Router,
@@ -38,8 +40,11 @@ export class BitstreamDownloadPageComponent implements OnInit {
3840
private hardRedirectService: HardRedirectService,
3941
private location: Location,
4042
public dsoNameService: DSONameService,
43+
private signpostingDataService: SignpostingDataService,
44+
private responseService: ServerResponseService,
45+
@Inject(PLATFORM_ID) protected platformId: string
4146
) {
42-
47+
this.initPageLinks();
4348
}
4449

4550
back(): void {
@@ -89,4 +94,26 @@ export class BitstreamDownloadPageComponent implements OnInit {
8994
}
9095
});
9196
}
97+
98+
/**
99+
* Create page links if any are retrieved by signposting endpoint
100+
*
101+
* @private
102+
*/
103+
private initPageLinks(): void {
104+
if (isPlatformServer(this.platformId)) {
105+
this.route.params.subscribe(params => {
106+
this.signpostingDataService.getLinks(params.id).pipe(take(1)).subscribe((signpostingLinks: SignpostingLink[]) => {
107+
let links = '';
108+
109+
signpostingLinks.forEach((link: SignpostingLink) => {
110+
links = links + (isNotEmpty(links) ? ', ' : '') + `<${link.href}> ; rel="${link.rel}"` + (isNotEmpty(link.type) ? ` ; type="${link.type}" ` : ' ');
111+
links = links + (isNotEmpty(links) ? ', ' : '') + `<${link.href}> ; rel="${link.rel}" ; type="${link.type}" `;
112+
});
113+
114+
this.responseService.setHeader('Link', links);
115+
});
116+
});
117+
}
118+
}
92119
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
2+
import { SignpostingDataService } from './signposting-data.service';
3+
import { DspaceRestService } from '../dspace-rest/dspace-rest.service';
4+
import { HALEndpointService } from '../shared/hal-endpoint.service';
5+
import { of } from 'rxjs';
6+
import { SignpostingLink } from './signposting-links.model';
7+
8+
describe('SignpostingDataService', () => {
9+
let service: SignpostingDataService;
10+
let restServiceSpy: jasmine.SpyObj<DspaceRestService>;
11+
let halServiceSpy: jasmine.SpyObj<HALEndpointService>;
12+
const mocklink = {
13+
href: 'http://test.org',
14+
rel: 'test',
15+
type: 'test'
16+
};
17+
18+
const mocklink2 = {
19+
href: 'http://test2.org',
20+
rel: 'test',
21+
type: 'test'
22+
};
23+
24+
const mockResponse: any = {
25+
statusCode: 200,
26+
payload: [mocklink, mocklink2]
27+
};
28+
29+
const mockErrResponse: any = {
30+
statusCode: 500
31+
};
32+
33+
beforeEach(() => {
34+
const restSpy = jasmine.createSpyObj('DspaceRestService', ['get', 'getWithHeaders']);
35+
const halSpy = jasmine.createSpyObj('HALEndpointService', ['getRootHref']);
36+
37+
TestBed.configureTestingModule({
38+
providers: [
39+
SignpostingDataService,
40+
{ provide: DspaceRestService, useValue: restSpy },
41+
{ provide: HALEndpointService, useValue: halSpy }
42+
]
43+
});
44+
45+
service = TestBed.inject(SignpostingDataService);
46+
restServiceSpy = TestBed.inject(DspaceRestService) as jasmine.SpyObj<DspaceRestService>;
47+
halServiceSpy = TestBed.inject(HALEndpointService) as jasmine.SpyObj<HALEndpointService>;
48+
});
49+
50+
it('should be created', () => {
51+
expect(service).toBeTruthy();
52+
});
53+
54+
it('should return signposting links', fakeAsync(() => {
55+
const uuid = '123';
56+
const baseUrl = 'http://localhost:8080';
57+
58+
halServiceSpy.getRootHref.and.returnValue(`${baseUrl}/api`);
59+
60+
restServiceSpy.get.and.returnValue(of(mockResponse));
61+
62+
let result: SignpostingLink[];
63+
64+
const expectedResult: SignpostingLink[] = [mocklink, mocklink2];
65+
66+
service.getLinks(uuid).subscribe((links) => {
67+
result = links;
68+
});
69+
70+
tick();
71+
72+
expect(result).toEqual(expectedResult);
73+
expect(halServiceSpy.getRootHref).toHaveBeenCalled();
74+
expect(restServiceSpy.get).toHaveBeenCalledWith(`${baseUrl}/signposting/links/${uuid}`);
75+
}));
76+
77+
it('should handle error and return an empty array', fakeAsync(() => {
78+
const uuid = '123';
79+
const baseUrl = 'http://localhost:8080';
80+
81+
halServiceSpy.getRootHref.and.returnValue(`${baseUrl}/api`);
82+
83+
restServiceSpy.get.and.returnValue(of(mockErrResponse));
84+
85+
let result: any;
86+
87+
service.getLinks(uuid).subscribe((data) => {
88+
result = data;
89+
});
90+
91+
tick();
92+
93+
expect(result).toEqual([]);
94+
expect(halServiceSpy.getRootHref).toHaveBeenCalled();
95+
expect(restServiceSpy.get).toHaveBeenCalledWith(`${baseUrl}/signposting/links/${uuid}`);
96+
}));
97+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Injectable } from '@angular/core';
2+
3+
import { catchError, map } from 'rxjs/operators';
4+
import { Observable, of as observableOf } from 'rxjs';
5+
6+
import { DspaceRestService } from '../dspace-rest/dspace-rest.service';
7+
import { HALEndpointService } from '../shared/hal-endpoint.service';
8+
import { RawRestResponse } from '../dspace-rest/raw-rest-response.model';
9+
import { SignpostingLink } from './signposting-links.model';
10+
11+
/**
12+
* Service responsible for handling requests related to the Signposting endpoint
13+
*/
14+
@Injectable({
15+
providedIn: 'root'
16+
})
17+
export class SignpostingDataService {
18+
19+
constructor(private restService: DspaceRestService, protected halService: HALEndpointService) {
20+
}
21+
22+
/**
23+
* Retrieve the list of signposting links related to the given resource's id
24+
*
25+
* @param uuid
26+
*/
27+
getLinks(uuid: string): Observable<SignpostingLink[]> {
28+
const baseUrl = this.halService.getRootHref().replace('/api', '');
29+
30+
return this.restService.get(`${baseUrl}/signposting/links/${uuid}`).pipe(
31+
catchError((err) => {
32+
return observableOf([]);
33+
}),
34+
map((res: RawRestResponse) => res.statusCode === 200 ? res.payload as SignpostingLink[] : [])
35+
);
36+
}
37+
38+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Represents the link object received by the signposting endpoint
3+
*/
4+
export interface SignpostingLink {
5+
href?: string,
6+
rel?: string,
7+
type?: string
8+
}

src/app/core/services/server-hard-redirect.service.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export class ServerHardRedirectService extends HardRedirectService {
4646
}
4747

4848
console.log(`Redirecting from ${this.req.url} to ${url} with ${status}`);
49+
4950
this.res.redirect(status, url);
5051
this.res.end();
5152
// I haven't found a way to correctly stop Angular rendering.

0 commit comments

Comments
 (0)