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;
}
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("&", "&");
//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;
}
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("&", "&");
//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;
}
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);
}
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;
}
Aggregations