Skip to content
UriService.java 1.93 KiB
Newer Older
package it.inaf.oats.vospace;

import it.inaf.oats.vospace.persistence.NodeDAO;
import java.util.ArrayList;
import java.util.List;
import net.ivoa.xml.uws.v1.JobSummary;
import net.ivoa.xml.uws.v1.ResultReference;
import net.ivoa.xml.vospace.v2.Node;
import net.ivoa.xml.vospace.v2.Protocol;
import net.ivoa.xml.vospace.v2.Transfer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class UriService {

    @Value("${vospace-authority}")
    private String authority;

    @Value("${file-service-url}")
    private String fileServiceUrl;

    @Autowired
    private NodeDAO nodeDao;

    public void setTransferJobResult(JobSummary job) {

        List<ResultReference> results = new ArrayList<>();

        ResultReference result = new ResultReference();
        result.setHref(getEndpoint(job));
        results.add(result);

        job.setResults(results);
    }

    public void setSyncTransferEndpoints(JobSummary job) {
        Transfer transfer = getTransfer(job);

        Protocol protocol = new Protocol();
        protocol.setUri("ivo://ivoa.net/vospace/core#httpget");
        protocol.setEndpoint(getEndpoint(job));

        transfer.getProtocols().add(protocol);
    }

    private String getEndpoint(JobSummary job) {

        Transfer transfer = getTransfer(job);

        String relativePath = transfer.getTarget().substring("vos://".length() + authority.length());

        // TODO handle node not found
        Node node = nodeDao.listNode(relativePath).get();

        // TODO build the path according to node type
        //
        // TODO add token for authenticated access
        return fileServiceUrl + relativePath + "?jobId=" + job.getJobId();
    }

    private Transfer getTransfer(JobSummary job) {
        // TODO add checks on data type
        return (Transfer) job.getJobInfo().getAny().get(0);
    }