[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) { }
getLinks(uuid: string): Observable<any> {
const url = this.halService.getRootHref().split('/');
const baseUrl = `${url[0]}//${url[2]}/${url[3]}`;
const baseUrl = this.halService.getRootHref().replace('/api', '');
return this.restService.get(`${baseUrl}/signposting/links/${uuid}`).pipe(
catchError((err ) => {
@@ -28,9 +27,8 @@ export class SignpostingDataService {
);
}
getLinksets(uuid: string): Observable<any> {
const url = this.halService.getRootHref().split('/');
const baseUrl = `${url[0]}//${url[2]}/${url[3]}`;
getLinksets(uuid: string): Observable<string> {
const baseUrl = this.halService.getRootHref().replace('/api', '');
const requestOptions = {
observe: 'response' as any,
@@ -46,7 +44,7 @@ export class SignpostingDataService {
console.error(err);
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.setCitationPublisherTag();
this.setSignpostingLinks();
this.setSignpostingLinksets();
if (this.isDissertation()) {
this.setCitationDissertationNameTag();
@@ -211,21 +210,24 @@ export class MetadataService {
console.log(link);
}
private setSignpostingLinksets() {
if (this.currentObject.value instanceof Item){
const value = this.signpostginDataService.getLinksets(this.currentObject.getValue().id);
value.subscribe(linksets => {
this.setLinkAttribute(linksets);
});
}
}
// public setSignpostingLinksets(itemId: string) {
// let linkSet: string;
setLinkAttribute(linksets){
console.log('ANDREA', linksets);
const linkAttribute = `Link: ${linksets.payload.body}`;
const textNode = document.createTextNode(linkAttribute);
document.head.appendChild(textNode);
}
// const value = this.signpostginDataService.getLinksets(itemId);
// value.subscribe(linksets => {
// linkSet = linksets.payload.body;
// });
// 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>

View File

@@ -2,6 +2,9 @@ import { Inject, Injectable } from '@angular/core';
import { Request, Response } from 'express';
import { REQUEST, RESPONSE } from '@nguniversal/express-engine/tokens';
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
@@ -12,6 +15,8 @@ export class ServerHardRedirectService extends HardRedirectService {
constructor(
@Inject(REQUEST) protected req: Request,
@Inject(RESPONSE) protected res: Response,
private signpostginDataService: SignpostingDataService,
protected route: ActivatedRoute
) {
super();
}
@@ -46,6 +51,13 @@ export class ServerHardRedirectService extends HardRedirectService {
}
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.end();
// 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 {
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 { Location } from '@angular/common';
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,
authService: AuthService,
authorizationService: AuthorizationDataService,
private _location: Location) {
super(route, router, items, authService, authorizationService);
private _location: Location,
responseService: ServerResponseService,
signpostginDataService: SignpostingDataService) {
super(route, router, items, authService, authorizationService, responseService, signpostginDataService);
}
/*** 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 { AuthorizationDataService } from '../../core/data/feature-authorization/authorization-data.service';
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.
@@ -62,8 +64,15 @@ export class ItemPageComponent implements OnInit {
private router: Router,
private items: ItemDataService,
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);
});
});
}
/**