Search in sources :

Example 11 with CabinetException

use of cz.metacentrum.perun.cabinet.bl.CabinetException in project perun by CESNET.

the class AbstractPublicationSystemStrategy method execute.

@Override
public HttpResponse execute(HttpUriRequest request) throws CabinetException {
    final HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 300000);
    HttpConnectionParams.setSoTimeout(httpParams, 300000);
    HttpClient httpClient = new DefaultHttpClient(httpParams);
    HttpResponse response = null;
    try {
        log.debug("Attempting to execute HTTP request...");
        response = httpClient.execute(request);
        log.debug("HTTP request executed.");
    } catch (IOException ioe) {
        log.error("Failed to execute HTTP request.");
        throw new CabinetException(ErrorCodes.HTTP_IO_EXCEPTION, ioe);
    }
    return response;
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) IOException(java.io.IOException) BasicHttpParams(org.apache.http.params.BasicHttpParams) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 12 with CabinetException

use of cz.metacentrum.perun.cabinet.bl.CabinetException in project perun by CESNET.

the class MUStrategy method parseResponse.

/**
	 * Parse String response as XML document and retrieve Publications from it.
	 * @param xml XML response from MU Prezentator
	 * @return List of Publications
	 * @throws CabinetException If anything fails
	 */
protected List<Publication> parseResponse(String xml) throws CabinetException {
    assert xml != null;
    List<Publication> result = new ArrayList<Publication>();
    //hook for titles with &
    xml = xml.replace("&", "&amp;");
    //log.debug("RESPONSE: "+xml);
    //Create new document factory builder
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        throw new CabinetException("Error when creating newDocumentBuilder.", ex);
    }
    Document doc;
    try {
        doc = builder.parse(new InputSource(new StringReader(xml)));
    } catch (SAXParseException ex) {
        throw new CabinetException("Error when parsing uri by document builder.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
    } catch (SAXException ex) {
        throw new CabinetException("Problem with parsing is more complex, not only invalid characters.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
    } catch (IOException ex) {
        throw new CabinetException("Error when parsing uri by document builder. Problem with input or output.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
    }
    //Prepare xpath expression
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression publicationsQuery;
    try {
        publicationsQuery = xpath.compile("/P/UL/publication");
    } catch (XPathExpressionException ex) {
        throw new CabinetException("Error when compiling xpath query.", ex);
    }
    NodeList nodeList;
    try {
        nodeList = (NodeList) publicationsQuery.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException ex) {
        throw new CabinetException("Error when evaluate xpath query on document.", ex);
    }
    //Test if there is any nodeset in result
    if (nodeList.getLength() == 0) {
        //There is no results, return empty subjects
        return result;
    }
    //Iterate through nodes and convert them to Map<String,String>
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node singleNode = nodeList.item(i);
        // remove node from original structure in order to keep access time constant (otherwise is exp.)
        singleNode.getParentNode().removeChild(singleNode);
        try {
            Publication publication = convertNodeToPublication(singleNode);
            result.add(publication);
        } catch (InternalErrorException ex) {
            log.error("Unable to parse Publication:", ex);
        }
    }
    return result;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Publication(cz.metacentrum.perun.cabinet.model.Publication) IOException(java.io.IOException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 13 with CabinetException

use of cz.metacentrum.perun.cabinet.bl.CabinetException in project perun by CESNET.

the class OBD30Strategy method parseResponse.

/**
	 * Parse String response as XML document and retrieve Publications from it.
	 * @param xml XML response from OBD 3.0
	 * @return List of Publications
	 * @throws CabinetException If anything fails
	 */
protected List<Publication> parseResponse(String xml) throws CabinetException {
    assert xml != null;
    List<Publication> result = new ArrayList<Publication>();
    //hook for titles with &
    xml = xml.replace("&", "&amp;");
    //log.debug("RESPONSE: "+xml);
    //Create new document factory builder
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder;
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        throw new CabinetException("Error when creating newDocumentBuilder.", ex);
    }
    Document doc;
    try {
        doc = builder.parse(new InputSource(new StringReader(xml)));
    } catch (SAXParseException ex) {
        throw new CabinetException("Error when parsing uri by document builder.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
    } catch (SAXException ex) {
        throw new CabinetException("Problem with parsing is more complex, not only invalid characters.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
    } catch (IOException ex) {
        throw new CabinetException("Error when parsing uri by document builder. Problem with input or output.", ErrorCodes.MALFORMED_HTTP_RESPONSE, ex);
    }
    //Prepare xpath expression
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression publicationsQuery;
    try {
        publicationsQuery = xpath.compile("/zaznamy/zaznam");
    } catch (XPathExpressionException ex) {
        throw new CabinetException("Error when compiling xpath query.", ex);
    }
    NodeList nodeList;
    try {
        nodeList = (NodeList) publicationsQuery.evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException ex) {
        throw new CabinetException("Error when evaluate xpath query on document.", ex);
    }
    //Test if there is any nodeset in result
    if (nodeList.getLength() == 0) {
        //There is no results, return empty subjects
        return result;
    }
    //Iterate through nodes and convert them to Map<String,String>
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node singleNode = nodeList.item(i);
        // remove node from original structure in order to keep access time constant (otherwise is exp.)
        singleNode.getParentNode().removeChild(singleNode);
        try {
            Publication publication = convertNodeToPublication(singleNode);
            result.add(publication);
        } catch (InternalErrorException ex) {
            log.error("Unable to parse Publication:", ex);
        }
    }
    return result;
}
Also used : XPath(javax.xml.xpath.XPath) XPathExpression(javax.xml.xpath.XPathExpression) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XPathExpressionException(javax.xml.xpath.XPathExpressionException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) Publication(cz.metacentrum.perun.cabinet.model.Publication) IOException(java.io.IOException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) XPathFactory(javax.xml.xpath.XPathFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) SAXParseException(org.xml.sax.SAXParseException) StringReader(java.io.StringReader) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 14 with CabinetException

use of cz.metacentrum.perun.cabinet.bl.CabinetException in project perun by CESNET.

the class AuthorshipManagerBlImpl method getAuthorsByAuthorshipId.

@Override
public List<Author> getAuthorsByAuthorshipId(PerunSession sess, int id) throws CabinetException, InternalErrorException {
    List<Author> result = new ArrayList<Author>();
    Authorship report = getAuthorshipManagerDao().getAuthorshipById(id);
    if (report == null) {
        throw new CabinetException("Authorship with ID: " + id + " doesn't exists!", ErrorCodes.AUTHORSHIP_NOT_EXISTS);
    }
    List<Authorship> publicationReports = getAuthorshipManagerDao().getAuthorshipsByPublicationId(report.getPublicationId());
    for (Authorship r : publicationReports) {
        result.add(getAuthorshipManagerDao().getAuthorById(r.getUserId()));
    }
    return convertAuthorsToAuthorsWithAttributes(result);
}
Also used : Authorship(cz.metacentrum.perun.cabinet.model.Authorship) ArrayList(java.util.ArrayList) Author(cz.metacentrum.perun.cabinet.model.Author) CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException)

Example 15 with CabinetException

use of cz.metacentrum.perun.cabinet.bl.CabinetException in project perun by CESNET.

the class AuthorshipManagerBlImpl method createAuthorship.

// business methods ===================================
@Override
public Authorship createAuthorship(PerunSession sess, Authorship authorship) throws CabinetException, InternalErrorException {
    if (authorshipExists(authorship))
        throw new CabinetException(ErrorCodes.AUTHORSHIP_ALREADY_EXISTS);
    if (authorship.getCreatedDate() == null) {
        authorship.setCreatedDate(new Date());
    }
    if (authorship.getCreatedByUid() == 0) {
        authorship.setCreatedByUid(sess.getPerunPrincipal().getUserId());
    }
    try {
        getAuthorshipManagerDao().createAuthorship(sess, authorship);
    } catch (DataIntegrityViolationException e) {
        throw new CabinetException(ErrorCodes.USER_NOT_EXISTS, e);
    }
    log.debug("{} created.", authorship);
    synchronized (CabinetManagerBlImpl.class) {
        getCabinetManagerBl().updatePriorityCoefficient(sess, authorship.getUserId(), calculateNewRank(authorship.getUserId()));
    }
    synchronized (ThanksManagerBlImpl.class) {
        getCabinetManagerBl().setThanksAttribute(authorship.getUserId());
    }
    // log
    perun.getAuditer().log(sess, "Authorship {} created.", authorship);
    return authorship;
}
Also used : CabinetException(cz.metacentrum.perun.cabinet.bl.CabinetException) Date(java.util.Date) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException)

Aggregations

CabinetException (cz.metacentrum.perun.cabinet.bl.CabinetException)19 Publication (cz.metacentrum.perun.cabinet.model.Publication)8 Date (java.util.Date)7 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 PerunException (cz.metacentrum.perun.core.api.exceptions.PerunException)4 Author (cz.metacentrum.perun.cabinet.model.Author)3 Authorship (cz.metacentrum.perun.cabinet.model.Authorship)3 PublicationSystem (cz.metacentrum.perun.cabinet.model.PublicationSystem)3 Thanks (cz.metacentrum.perun.cabinet.model.Thanks)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)3 IOException (java.io.IOException)3 HttpResponse (org.apache.http.HttpResponse)3 UserNotExistsException (cz.metacentrum.perun.core.api.exceptions.UserNotExistsException)2 StringReader (java.io.StringReader)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 XPath (javax.xml.xpath.XPath)2 XPathExpression (javax.xml.xpath.XPathExpression)2