[CST-5729] Implemented functionality to add Links in Response Headers on Item Page

This commit is contained in:
Alban Imami
2023-05-11 18:47:01 +02:00
parent e8ff0fbf36
commit 558f8f51fb
6 changed files with 55 additions and 24 deletions

View File

@@ -16,8 +16,7 @@ export class SignpostingDataService {
constructor(private restService: DspaceRestService, protected halService: HALEndpointService) { } constructor(private restService: DspaceRestService, protected halService: HALEndpointService) { }
getLinks(uuid: string): Observable<any> { getLinks(uuid: string): Observable<any> {
const url = this.halService.getRootHref().split('/'); const baseUrl = this.halService.getRootHref().replace('/api', '');
const baseUrl = `${url[0]}//${url[2]}/${url[3]}`;
return this.restService.get(`${baseUrl}/signposting/links/${uuid}`).pipe( return this.restService.get(`${baseUrl}/signposting/links/${uuid}`).pipe(
catchError((err ) => { catchError((err ) => {
@@ -28,9 +27,8 @@ export class SignpostingDataService {
); );
} }
getLinksets(uuid: string): Observable<any> { getLinksets(uuid: string): Observable<string> {
const url = this.halService.getRootHref().split('/'); const baseUrl = this.halService.getRootHref().replace('/api', '');
const baseUrl = `${url[0]}//${url[2]}/${url[3]}`;
const requestOptions = { const requestOptions = {
observe: 'response' as any, observe: 'response' as any,
@@ -46,7 +44,7 @@ export class SignpostingDataService {
console.error(err); console.error(err);
return observableOf(false); return observableOf(false);
}), }),
map((res: RawRestResponse) => res) map((res: RawRestResponse) => res.payload.body)
); );
} }
} }

View File

@@ -165,7 +165,6 @@ export class MetadataService {
this.setCitationPdfUrlTag(); this.setCitationPdfUrlTag();
this.setCitationPublisherTag(); this.setCitationPublisherTag();
this.setSignpostingLinks(); this.setSignpostingLinks();
this.setSignpostingLinksets();
if (this.isDissertation()) { if (this.isDissertation()) {
this.setCitationDissertationNameTag(); this.setCitationDissertationNameTag();
@@ -211,21 +210,24 @@ export class MetadataService {
console.log(link); console.log(link);
} }
private setSignpostingLinksets() { // public setSignpostingLinksets(itemId: string) {
if (this.currentObject.value instanceof Item){ // let linkSet: string;
const value = this.signpostginDataService.getLinksets(this.currentObject.getValue().id);
value.subscribe(linksets => {
this.setLinkAttribute(linksets);
});
}
}
setLinkAttribute(linksets){ // const value = this.signpostginDataService.getLinksets(itemId);
console.log('ANDREA', linksets);
const linkAttribute = `Link: ${linksets.payload.body}`; // value.subscribe(linksets => {
const textNode = document.createTextNode(linkAttribute); // linkSet = linksets.payload.body;
document.head.appendChild(textNode); // });
}
// return linkSet;
// }
// setLinkAttribute(linksets){
// console.log('ANDREA', linksets);
// const linkAttribute = `Link: ${linksets.payload.body}`;
// const textNode = document.createTextNode(linkAttribute);
// document.head.appendChild(textNode);
// }
/** /**
* Add <meta name="title" ... > to the <head> * Add <meta name="title" ... > to the <head>

View File

@@ -2,6 +2,9 @@ import { Inject, Injectable } from '@angular/core';
import { Request, Response } from 'express'; import { Request, Response } from 'express';
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens'; import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
import { HardRedirectService } from './hard-redirect.service'; import { HardRedirectService } from './hard-redirect.service';
import { SignpostingDataService } from '../data/signposting-data.service';
import { ActivatedRoute } from '@angular/router';
import { take } from 'rxjs';
/** /**
* Service for performing hard redirects within the server app module * Service for performing hard redirects within the server app module
@@ -12,6 +15,8 @@ export class ServerHardRedirectService extends HardRedirectService {
constructor( constructor(
@Inject(REQUEST) protected req: Request, @Inject(REQUEST) protected req: Request,
@Inject(RESPONSE) protected res: Response, @Inject(RESPONSE) protected res: Response,
private signpostginDataService: SignpostingDataService,
protected route: ActivatedRoute
) { ) {
super(); super();
} }
@@ -46,6 +51,13 @@ export class ServerHardRedirectService extends HardRedirectService {
} }
console.log(`Redirecting from ${this.req.url} to ${url} with ${status}`); console.log(`Redirecting from ${this.req.url} to ${url} with ${status}`);
this.route.params.subscribe(params => {
this.signpostginDataService.getLinksets(params.id).pipe(take(1)).subscribe(linksets => {
this.res.setHeader('Link', linksets);
});
});
this.res.redirect(status, url); this.res.redirect(status, url);
this.res.end(); this.res.end();
// I haven't found a way to correctly stop Angular rendering. // I haven't found a way to correctly stop Angular rendering.

View File

@@ -35,4 +35,10 @@ export class ServerResponseService {
setInternalServerError(message = 'Internal Server Error'): this { setInternalServerError(message = 'Internal Server Error'): this {
return this.setStatus(500, message); return this.setStatus(500, message);
} }
setLinksetsHeader(linksets: string){
if (this.response) {
this.response.setHeader('Link', linksets);
}
}
} }

View File

@@ -16,6 +16,8 @@ import { hasValue } from '../../shared/empty.util';
import { AuthService } from '../../core/auth/auth.service'; import { AuthService } from '../../core/auth/auth.service';
import { Location } from '@angular/common'; import { Location } from '@angular/common';
import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service'; import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service';
import { ServerResponseService } from 'src/app/core/services/server-response.service';
import { SignpostingDataService } from 'src/app/core/data/signposting-data.service';
/** /**
@@ -48,8 +50,10 @@ export class FullItemPageComponent extends ItemPageComponent implements OnInit,
items: ItemDataService, items: ItemDataService,
authService: AuthService, authService: AuthService,
authorizationService: AuthorizationDataService, authorizationService: AuthorizationDataService,
private _location: Location) { private _location: Location,
super(route, router, items, authService, authorizationService); responseService: ServerResponseService,
signpostginDataService: SignpostingDataService) {
super(route, router, items, authService, authorizationService, responseService, signpostginDataService);
} }
/*** AoT inheritance fix, will hopefully be resolved in the near future **/ /*** AoT inheritance fix, will hopefully be resolved in the near future **/

View File

@@ -15,6 +15,8 @@ import { getItemPageRoute } from '../item-page-routing-paths';
import { redirectOn4xx } from '../../core/shared/authorized.operators'; import { redirectOn4xx } from '../../core/shared/authorized.operators';
import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service'; import { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service';
import { FeatureID } from '../../core/data/feature-authorization/feature-id'; import { FeatureID } from '../../core/data/feature-authorization/feature-id';
import { ServerResponseService } from '../../core/services/server-response.service';
import { SignpostingDataService } from '../../core/data/signposting-data.service';
/** /**
* This component renders a simple item page. * This component renders a simple item page.
@@ -62,8 +64,15 @@ export class ItemPageComponent implements OnInit {
private router: Router, private router: Router,
private items: ItemDataService, private items: ItemDataService,
private authService: AuthService, private authService: AuthService,
private authorizationService: AuthorizationDataService private authorizationService: AuthorizationDataService,
private responseService: ServerResponseService,
private signpostginDataService: SignpostingDataService
) { ) {
this.route.params.subscribe(params => {
this.signpostginDataService.getLinksets(params.id).subscribe(linksets => {
this.responseService.setLinksetsHeader(linksets);
});
});
} }
/** /**