<commit_before>package dk.statsbiblioteket.doms.ecm.repository.fedoraclient;

import dk.statsbiblioteket.doms.ecm.repository.FedoraConnector;
import dk.statsbiblioteket.doms.ecm.repository.FedoraUserToken;
import dk.statsbiblioteket.doms.ecm.repository.PidList;
import dk.statsbiblioteket.doms.ecm.repository.exceptions.*;
import dk.statsbiblioteket.doms.ecm.repository.utils.Constants;
import dk.statsbiblioteket.doms.ecm.repository.utils.DocumentUtils;
import dk.statsbiblioteket.doms.ecm.repository.utils.FedoraUtil;
import org.apache.axis.AxisFault;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.fcrepo.client.FedoraClient;
import org.fcrepo.server.access.FedoraAPIA;
import org.fcrepo.server.errors.LowlevelStorageException;
import org.fcrepo.server.errors.authorization.AuthzException;
import org.fcrepo.server.management.FedoraAPIM;
import org.fcrepo.server.types.gen.DatastreamDef;
import org.fcrepo.server.types.gen.MIMETypedStream;
import org.fcrepo.server.types.gen.ObjectProfile;
import org.fcrepo.server.types.gen.RelationshipTuple;
import org.jrdf.graph.Node;
import org.trippi.TrippiException;
import org.trippi.TupleIterator;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.rpc.ServiceException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.*;


/**
 * This is a implementation of the Fedora connector, based on the old
 * FedoraClient This FedoraClient is really a wrapping of the soap api.
 */
public class FedoraClientConnector
        implements FedoraConnector {


    private static final Log LOG = LogFactory.getLog(FedoraClientConnector.class);
    private FedoraUserToken token;

    //Do not get this directly, use the accessor
    private FedoraClient client;
    private FedoraAPIM apiM;
    private FedoraAPIA apiA;

    //noargs constructor, as required
    public FedoraClientConnector() {

    }


    public void initialise(FedoraUserToken token) {
        this.token = token;
        apiA = null;
        apiM = null;
        client = null;
    }


    public boolean addRelation(String from, String relation, String to)
            throws ObjectNotFoundException, FedoraConnectionException,
                   FedoraIllegalContentException, InvalidCredentialsException {
        from = FedoraUtil.ensurePID(from);
        to = FedoraUtil.ensureURI(to);
        try {
            return getAPIM().addRelationship(from, relation, to, false, null);
        } catch (RemoteException e) {
            if (e instanceof AxisFault) {
                AxisFault axisFault = (AxisFault) e;
                if (axisFault.getCause() instanceof AuthzException) {
                    AuthzException authzException
                            = (AuthzException) axisFault.getCause();
                    throw new InvalidCredentialsException(
                            "The supplied credentials were insufficient for the"
                            + " task at hand",authzException);
                }
                if (axisFault.getCause() instanceof LowlevelStorageException) {
                    LowlevelStorageException lowlevelStorageException
                            = (LowlevelStorageException) axisFault.getCause();
                    throw new ObjectNotFoundException("The object '"+from+"' was not found",axisFault.getCause());
                }

            }
            throw new FedoraConnectionException(
                    "Something went wrong in the connection with fedora",
                    e);
        }
    }

    public boolean addLiteralRelation(String from,
                                      String relation,
                                      String value,
                                      String datatype)
            throws ObjectNotFoundException, FedoraConnectionException,
                   FedoraIllegalContentException, InvalidCredentialsException {
        from = FedoraUtil.ensurePID(from);

     
        try {
            return getAPIM().addRelationship(from,
                                             relation,
                                             value,
                                             true,
                                             datatype);
        } catch (RemoteException e) {
            if (e instanceof AxisFault) {
                AxisFault axisFault = (AxisFault) e;
                if (axisFault.getCause() instanceof AuthzException) {
                    AuthzException authzException
                            = (AuthzException) axisFault.getCause();
                    throw new InvalidCredentialsException(
                            "The supplied credentials were insufficient for the"
                            + " task at hand",authzException);
                }
                if (axisFault.getCause() instanceof LowlevelStorageException) {
                    LowlevelStorageException lowlevelStorageException
                            = (LowlevelStorageException) axisFault.getCause();
                    throw new ObjectNotFoundException("The object '"+from+"' was not found",axisFault.getCause());
                }
            }
            throw new FedoraConnectionException(
                    "Something went wrong in the connection with fedora",
                    e);
        }
    }

    public List<Relation> getRelations(String pid)
            throws ObjectNotFoundException, FedoraConnectionException,
                   FedoraIllegalContentException, InvalidCredentialsException {
        return getRelations(pid, null);
    }

    public List<Relation> getRelations(String pid, String relation)
            throws ObjectNotFoundException, FedoraConnectionException,
                   FedoraIllegalContentException, InvalidCredentialsException {
        pid = FedoraUtil.ensurePID(pid);
        try {
            RelationshipTuple[] relations = getAPIM().getRelationships(pid,
                                                                       relation);
            List<Relation> result = new ArrayList<Relation>();
            if (relations != null) {

                for (RelationshipTuple rel : relations) {
                    result.add(toRelation(rel));
                }

            }
            return result;
        } catch (RemoteException e) {
            if (e instanceof AxisFault) {
                AxisFault axisFault = (AxisFault) e;
                if (axisFault.getCause() instanceof AuthzException) {
                    AuthzException authzException
                            = (AuthzException) axisFault.getCause();
                    throw new InvalidCredentialsException(
                            "The supplied credentials were insufficient for the"
                            + " task at hand",authzException);
                }
                if (axisFault.getCause() instanceof LowlevelStorageException) {
                    LowlevelStorageException lowlevelStorageException
                            = (LowlevelStorageException) axisFault.getCause();
                    throw new ObjectNotFoundException("The object '"+pid+"' was not found",axisFault.getCause());
                }
            }<commit_msg>Changed so that relation methods are based uri, not pid. This should prevent the many many errors from the fedora logs<commit_after>package dk.statsbiblioteket.doms.ecm.repository.fedoraclient;

import dk.statsbiblioteket.doms.ecm.repository.FedoraConnector;
import dk.statsbiblioteket.doms.ecm.repository.FedoraUserToken;
import dk.statsbiblioteket.doms.ecm.repository.PidList;
import dk.statsbiblioteket.doms.ecm.repository.exceptions.*;
import dk.statsbiblioteket.doms.ecm.repository.utils.Constants;
import dk.statsbiblioteket.doms.ecm.repository.utils.DocumentUtils;
import dk.statsbiblioteket.doms.ecm.repository.utils.FedoraUtil;
import org.apache.axis.AxisFault;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.fcrepo.client.FedoraClient;
import org.fcrepo.server.access.FedoraAPIA;
import org.fcrepo.server.errors.LowlevelStorageException;
import org.fcrepo.server.errors.authorization.AuthzException;
import org.fcrepo.server.management.FedoraAPIM;
import org.fcrepo.server.types.gen.DatastreamDef;
import org.fcrepo.server.types.gen.MIMETypedStream;
import org.fcrepo.server.types.gen.ObjectProfile;
import org.fcrepo.server.types.gen.RelationshipTuple;
import org.jrdf.graph.Node;
import org.trippi.TrippiException;
import org.trippi.TupleIterator;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.rpc.ServiceException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
import java.util.*;


/**
 * This is a implementation of the Fedora connector, based on the old
 * FedoraClient This FedoraClient is really a wrapping of the soap api.
 */
public class FedoraClientConnector
        implements FedoraConnector {


    private static final Log LOG = LogFactory.getLog(FedoraClientConnector.class);
    private FedoraUserToken token;

    //Do not get this directly, use the accessor
    private FedoraClient client;
    private FedoraAPIM apiM;
    private FedoraAPIA apiA;

    //noargs constructor, as required
    public FedoraClientConnector() {

    }


    public void initialise(FedoraUserToken token) {
        this.token = token;
        apiA = null;
        apiM = null;
        client = null;
    }


    public boolean addRelation(String from, String relation, String to)
            throws ObjectNotFoundException, FedoraConnectionException,
                   FedoraIllegalContentException, InvalidCredentialsException {
        from = FedoraUtil.ensureURI(from);
        to = FedoraUtil.ensureURI(to);
        try {
            return getAPIM().addRelationship(from, relation, to, false, null);
        } catch (RemoteException e) {
            if (e instanceof AxisFault) {
                AxisFault axisFault = (AxisFault) e;
                if (axisFault.getCause() instanceof AuthzException) {
                    AuthzException authzException
                            = (AuthzException) axisFault.getCause();
                    throw new InvalidCredentialsException(
                            "The supplied credentials were insufficient for the"
                            + " task at hand",authzException);
                }
                if (axisFault.getCause() instanceof LowlevelStorageException) {
                    LowlevelStorageException lowlevelStorageException
                            = (LowlevelStorageException) axisFault.getCause();
                    throw new ObjectNotFoundException("The object '"+from+"' was not found",axisFault.getCause());
                }

            }
            throw new FedoraConnectionException(
                    "Something went wrong in the connection with fedora",
                    e);
        }
    }

    public boolean addLiteralRelation(String from,
                                      String relation,
                                      String value,
                                      String datatype)
            throws ObjectNotFoundException, FedoraConnectionException,
                   FedoraIllegalContentException, InvalidCredentialsException {
        from = FedoraUtil.ensureURI(from);

     
        try {
            return getAPIM().addRelationship(from,
                                             relation,
                                             value,
                                             true,
                                             datatype);
        } catch (RemoteException e) {
            if (e instanceof AxisFault) {
                AxisFault axisFault = (AxisFault) e;
                if (axisFault.getCause() instanceof AuthzException) {
                    AuthzException authzException
                            = (AuthzException) axisFault.getCause();
                    throw new InvalidCredentialsException(
                            "The supplied credentials were insufficient for the"
                            + " task at hand",authzException);
                }
                if (axisFault.getCause() instanceof LowlevelStorageException) {
                    LowlevelStorageException lowlevelStorageException
                            = (LowlevelStorageException) axisFault.getCause();
                    throw new ObjectNotFoundException("The object '"+from+"' was not found",axisFault.getCause());
                }
            }
            throw new FedoraConnectionException(
                    "Something went wrong in the connection with fedora",
                    e);
        }
    }

    public List<Relation> getRelations(String pid)
            throws ObjectNotFoundException, FedoraConnectionException,
                   FedoraIllegalContentException, InvalidCredentialsException {
        return getRelations(pid, null);
    }

    public List<Relation> getRelations(String pid, String relation)
            throws ObjectNotFoundException, FedoraConnectionException,
                   FedoraIllegalContentException, InvalidCredentialsException {
        pid = FedoraUtil.ensureURI(pid);
        try {
            RelationshipTuple[] relations = getAPIM().getRelationships(pid,
                                                                       relation);
            List<Relation> result = new ArrayList<Relation>();
            if (relations != null) {

                for (RelationshipTuple rel : relations) {
                    result.add(toRelation(rel));
                }

            }
            return result;
        } catch (RemoteException e) {
            if (e instanceof AxisFault) {
                AxisFault axisFault = (AxisFault) e;
                if (axisFault.getCause() instanceof AuthzException) {
                    AuthzException authzException
                            = (AuthzException) axisFault.getCause();
                    throw new InvalidCredentialsException(
                            "The supplied credentials were insufficient for the"
                            + " task at hand",authzException);
                }
                if (axisFault.getCause() instanceof LowlevelStorageException) {
                    LowlevelStorageException lowlevelStorageException
                            = (LowlevelStorageException) axisFault.getCause();
                    throw new ObjectNotFoundException("The object '"+pid+"' was not found",axisFault.getCause());
                }
            }
{'fraction_non_alphanumeric': 0.07215332581736189, 'fraction_numerical': 0.00013263479010544466, 'mean_word_length': 1.994440031771247, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6653844', 'n_tokens_mistral': 3745, 'n_tokens_neox': 3594, 'n_words': 824}