Refactors Single Dataset Enpoint to support different formats (JSON, XML, DOCX)
This commit is contained in:
parent
2ebd3f1cb8
commit
03bfbdf8ad
|
@ -1,5 +1,6 @@
|
|||
package eu.eudat.controllers;
|
||||
|
||||
import eu.eudat.data.dao.entities.DatasetDao;
|
||||
import eu.eudat.data.entities.Dataset;
|
||||
import eu.eudat.data.query.items.item.dataset.DatasetWizardAutocompleteRequest;
|
||||
import eu.eudat.data.query.items.item.datasetprofile.DatasetProfileWizardAutocompleteRequest;
|
||||
|
@ -9,6 +10,7 @@ import eu.eudat.logic.managers.DatasetWizardManager;
|
|||
import eu.eudat.logic.managers.UserManager;
|
||||
import eu.eudat.logic.security.claims.ClaimedAuthorities;
|
||||
import eu.eudat.logic.services.ApiContext;
|
||||
import eu.eudat.logic.services.forms.VisibilityRuleService;
|
||||
import eu.eudat.logic.utilities.documents.helpers.FileEnvelope;
|
||||
import eu.eudat.models.data.datasetwizard.DataManagentPlanListingModel;
|
||||
import eu.eudat.models.data.datasetwizard.DatasetWizardModel;
|
||||
|
@ -67,11 +69,19 @@ public class DatasetWizardController extends BaseController {
|
|||
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<List<AssociatedProfile>>().status(ApiMessageCode.NO_MESSAGE).payload(dataManagementPlans));
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = {"/getSingle/{id}"}, produces = "application/json")
|
||||
@RequestMapping(method = RequestMethod.GET, value = {"{id}"}, produces = "application/json")
|
||||
public @ResponseBody
|
||||
ResponseEntity<ResponseItem<DatasetWizardModel>> getSingle(@PathVariable String id, @ClaimedAuthorities(claims = {ANONYMOUS}) Principal principal) throws IllegalAccessException, IOException, InstantiationException {
|
||||
DatasetWizardModel dataset = this.datasetManager.getSingle(this.getApiContext().getOperationsContext().getDatabaseRepository().getDatasetDao(), this.getApiContext().getOperationsContext().getDatasetRepository(), id);
|
||||
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DatasetWizardModel>().status(ApiMessageCode.NO_MESSAGE).payload(dataset));
|
||||
ResponseEntity getSingle(@PathVariable String id, @RequestHeader("Content-Type") String contentType, @ClaimedAuthorities(claims = {ANONYMOUS}) Principal principal) throws IllegalAccessException, IOException, InstantiationException {
|
||||
if(contentType.equals("application/xml")){
|
||||
DatasetDao datasetDao = this.getApiContext().getOperationsContext().getDatabaseRepository().getDatasetDao();
|
||||
VisibilityRuleService visibilityRuleService = this.getApiContext().getUtilitiesService().getVisibilityRuleService();
|
||||
|
||||
return this.datasetManager.getDocument(datasetDao, id, visibilityRuleService, contentType);
|
||||
}
|
||||
else {
|
||||
DatasetWizardModel dataset = this.datasetManager.getSingle(this.getApiContext().getOperationsContext().getDatabaseRepository().getDatasetDao(), this.getApiContext().getOperationsContext().getDatasetRepository(), id);
|
||||
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DatasetWizardModel>().status(ApiMessageCode.NO_MESSAGE).payload(dataset));
|
||||
}
|
||||
}
|
||||
|
||||
@Transactional
|
||||
|
@ -111,26 +121,6 @@ public class DatasetWizardController extends BaseController {
|
|||
HttpStatus.OK);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = {"/getXml/{id}"})
|
||||
public @ResponseBody
|
||||
ResponseEntity<byte[]> getXml(@PathVariable String id) throws IllegalAccessException, IOException, InstantiationException {
|
||||
FileEnvelope envelope = new DatasetManager().getXmlDocument(this.getApiContext().getOperationsContext().getDatabaseRepository().getDatasetDao(), id, this.getApiContext().getUtilitiesService().getVisibilityRuleService());
|
||||
InputStream resource = new FileInputStream(envelope.getFile());
|
||||
System.out.println("Mime Type of " + envelope.getFilename() + " is " +
|
||||
new MimetypesFileTypeMap().getContentType(envelope.getFile()));
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
responseHeaders.setContentLength(envelope.getFile().length());
|
||||
responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
||||
responseHeaders.set("Content-Disposition", "attachment;filename=" + envelope.getFilename() + ".xml");
|
||||
responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition");
|
||||
responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type");
|
||||
|
||||
byte[] content = IOUtils.toByteArray(resource);
|
||||
return new ResponseEntity<>(content,
|
||||
responseHeaders,
|
||||
HttpStatus.OK);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = {"/get/{id}"}, produces = "application/json")
|
||||
public ResponseEntity<ResponseItem<PagedDatasetProfile>> getSingle(@PathVariable String id) {
|
||||
eu.eudat.data.entities.DatasetProfile profile = this.getApiContext().getOperationsContext().getDatabaseRepository().getDatasetProfileDao().find(UUID.fromString(id));
|
||||
|
|
|
@ -34,8 +34,10 @@ import org.springframework.stereotype.Component;
|
|||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import javax.activation.MimetypesFileTypeMap;
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
@ -375,4 +377,25 @@ public class DatasetManager {
|
|||
datasetDao.createOrUpdate(dataset);
|
||||
}
|
||||
|
||||
public ResponseEntity<byte[]> getDocument(DatasetDao datasetDao, String id, VisibilityRuleService visibilityRuleService, String contentType) throws IllegalAccessException, IOException, InstantiationException {
|
||||
FileEnvelope envelope = getXmlDocument(datasetDao, id, visibilityRuleService);
|
||||
InputStream resource = new FileInputStream(envelope.getFile());
|
||||
System.out.println("Mime Type of " + envelope.getFilename() + " is " +
|
||||
new MimetypesFileTypeMap().getContentType(envelope.getFile()));
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
responseHeaders.setContentLength(envelope.getFile().length());
|
||||
responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
||||
responseHeaders.set("Content-Disposition", "attachment;filename=" + envelope.getFilename() + ".xml");
|
||||
responseHeaders.set("Access-Control-Expose-Headers", "Content-Disposition");
|
||||
responseHeaders.get("Access-Control-Expose-Headers").add("Content-Type");
|
||||
|
||||
byte[] content = org.apache.poi.util.IOUtils.toByteArray(resource);
|
||||
resource.close();
|
||||
Files.deleteIfExists(envelope.getFile().toPath());
|
||||
|
||||
return new ResponseEntity<>(content,
|
||||
responseHeaders,
|
||||
HttpStatus.OK);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ import { BaseHttpService } from '../http/base-http.service';
|
|||
export class DatasetWizardService {
|
||||
|
||||
private actionUrl: string;
|
||||
private headers: HttpHeaders;
|
||||
private headers = new HttpHeaders();
|
||||
|
||||
constructor(private http: BaseHttpService, private httpClient: HttpClient) {
|
||||
this.actionUrl = environment.Server + 'datasetwizard/';
|
||||
|
@ -30,7 +30,7 @@ export class DatasetWizardService {
|
|||
}
|
||||
|
||||
public getSingle(id: String): Observable<DatasetWizardModel> {
|
||||
return this.http.get<DatasetWizardModel>(this.actionUrl + 'getSingle/' + id, { headers: this.headers });
|
||||
return this.http.get<DatasetWizardModel>(this.actionUrl + id, { headers: this.headers }); // + 'getSingle/'
|
||||
}
|
||||
|
||||
public delete(id: string): Observable<DatasetWizardModel> {
|
||||
|
@ -42,11 +42,12 @@ export class DatasetWizardService {
|
|||
}
|
||||
|
||||
public downloadPDF(id: string): Observable<HttpResponse<Blob>> {
|
||||
return this.httpClient.get(this.actionUrl + 'getPDF/' + id, { responseType: 'blob', observe: 'response' });
|
||||
return this.httpClient.get(this.actionUrl + 'getPDF/' + id, { responseType: 'blob', observe: 'response', headers: this.headers });
|
||||
}
|
||||
|
||||
public downloadXML(id: string): Observable<HttpResponse<Blob>> {
|
||||
return this.httpClient.get(this.actionUrl + 'getXml/' + id, { responseType: 'blob', observe: 'response' });
|
||||
let headerXml: HttpHeaders = this.headers.set('Content-Type', 'application/xml')
|
||||
return this.httpClient.get(this.actionUrl + id, { responseType: 'blob', observe: 'response', headers: headerXml }); // + 'getXml/'
|
||||
}
|
||||
|
||||
public getDefinition(id: String): Observable<DatasetProfileDefinitionModel> {
|
||||
|
|
Loading…
Reference in New Issue