Commit 39cbc4ac authored by Matija Obreza's avatar Matija Obreza
Browse files

Exceptions for EasySMTA service

parent 37b12081
......@@ -18,6 +18,8 @@ package org.genesys2.server.service;
import java.io.IOException;
import org.genesys2.server.service.impl.EasySMTAException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
......@@ -27,7 +29,7 @@ import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
public interface EasySMTA {
EasySMTAUserData getUserData(String emailAddress);
EasySMTAUserData getUserData(String emailAddress) throws EasySMTAException;
/**
* Data returned by Easy-SMTA
......
......@@ -16,13 +16,13 @@
package org.genesys2.server.service;
import java.io.IOException;
import java.util.Set;
import org.genesys2.server.model.genesys.MaterialRequest;
import org.genesys2.server.model.genesys.MaterialSubRequest;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.TokenVerificationService.NoSuchVerificationTokenException;
import org.genesys2.server.service.impl.EasySMTAException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
......@@ -30,7 +30,7 @@ public interface RequestService {
/**
* Creates a {@link MaterialRequest} and sends a validation email to user
*
*
* @param requestInfo
* @param accessionIds
* @return
......@@ -40,7 +40,7 @@ public interface RequestService {
/**
* Validation request attempt by user
*
*
* @param tokenUuid
* @param key
* @return
......@@ -48,12 +48,14 @@ public interface RequestService {
* On request processing exception (e.g. missing PID data)
* @throws NoSuchVerificationTokenException
* If verification token is invalid
* @throws NoPidException
* @throws EasySMTAException
*/
MaterialRequest validateClientRequest(String tokenUuid, String key) throws RequestException, NoSuchVerificationTokenException;
MaterialRequest validateClientRequest(String tokenUuid, String key) throws RequestException, NoSuchVerificationTokenException, NoPidException, EasySMTAException;
/**
* Relay sub-request to holding institute
*
*
* @param materialSubRequest
*/
void relayRequest(MaterialSubRequest materialSubRequest);
......@@ -73,17 +75,12 @@ public interface RequestService {
super(message);
}
public RequestException(String message, IOException e) {
public RequestException(String message, Throwable e) {
super(message, e);
}
}
static class NoPidException extends RuntimeException {
/**
*
*/
private static final long serialVersionUID = 8218041860499089421L;
static class NoPidException extends Exception {
public NoPidException(String message) {
super(message);
}
......@@ -140,19 +137,22 @@ public interface RequestService {
/**
* Recheck Easy-SMTA for PID
*
*
* @param materialRequest
* @throws NoPidException
* @return
* @throws EasySMTAException
*/
MaterialRequest recheckPid(MaterialRequest materialRequest);
MaterialRequest recheckPid(MaterialRequest materialRequest) throws NoPidException, EasySMTAException;
/**
* Allow admin to validate request (recheck PID, relay)
*
*
* @param materialRequest
* @return
* @throws EasySMTAException
* @throws NoPidException
*/
MaterialRequest validateRequest(MaterialRequest materialRequest);
MaterialRequest validateRequest(MaterialRequest materialRequest) throws NoPidException, EasySMTAException;
}
......@@ -22,6 +22,8 @@ import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.SSLPeerUnverifiedException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
......@@ -57,7 +59,7 @@ public class EasySMTAConnector implements EasySMTA {
private String serverPassword;
@Override
public EasySMTA.EasySMTAUserData getUserData(String emailAddress) {
public EasySMTA.EasySMTAUserData getUserData(String emailAddress) throws EasySMTAException {
final HttpClient httpclient = new DefaultHttpClient();
LOG.info("Checking EasySMTA at " + serverUrl + " for email: " + emailAddress);
......@@ -84,8 +86,10 @@ public class EasySMTAConnector implements EasySMTA {
// Get hold of the response entity
final HttpEntity entity = response.getEntity();
LOG.debug(entity.getContentType() + " " + entity.getContentLength());
if (LOG.isDebugEnabled()) {
LOG.debug(entity.getContentType() + " " + entity.getContentLength());
}
// If the response does not enclose an entity, there is no
// need to bother about connection release
if (entity != null) {
......@@ -103,11 +107,14 @@ public class EasySMTAConnector implements EasySMTA {
return objectMapper.readValue(tree.toString(), EasySMTA.EasySMTAUserData.class);
}
}
} catch (final SSLPeerUnverifiedException e) {
throw new EasySMTAException("Secure connection to EasySMTA failed. Certificate invalid.", e);
} catch (final ClientProtocolException e) {
LOG.error(e.getMessage(), e);
throw new EasySMTAException(e.getMessage(), e);
} catch (final IOException e) {
LOG.error(e);
throw new EasySMTAException(e.getMessage(), e);
} finally {
IOUtils.closeQuietly(br);
httpPost.releaseConnection();
......@@ -116,4 +123,5 @@ public class EasySMTAConnector implements EasySMTA {
return null;
}
}
}
\ No newline at end of file
package org.genesys2.server.service.impl;
public class EasySMTAException extends Exception {
public EasySMTAException(String string, Throwable e) {
super(string, e);
}
}
......@@ -110,7 +110,12 @@ public class RequestServiceImpl implements RequestService {
}
// Check Easy-SMTA for PID
final EasySMTA.EasySMTAUserData pid = pidChecker.getUserData(requestInfo.getEmail());
EasySMTA.EasySMTAUserData pid;
try {
pid = pidChecker.getUserData(requestInfo.getEmail());
} catch (EasySMTAException e) {
throw new RequestException(e.getMessage(), e);
}
final MaterialRequest request = createRequest(requestInfo, pid, availableAccessionIds);
......@@ -197,7 +202,7 @@ public class RequestServiceImpl implements RequestService {
// Rollback for any exception
@Override
@Transactional
public MaterialRequest validateClientRequest(String tokenUuid, String key) throws NoSuchVerificationTokenException {
public MaterialRequest validateClientRequest(String tokenUuid, String key) throws NoSuchVerificationTokenException, NoPidException, EasySMTAException {
final VerificationToken consumedToken = tokenVerificationService.consumeToken(REQUEST_TOKENTYPE, tokenUuid, key);
final MaterialRequest materialRequest = requestRepository.findByUuid(consumedToken.getData());
......@@ -236,9 +241,10 @@ public class RequestServiceImpl implements RequestService {
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR')")
public MaterialRequest recheckPid(MaterialRequest materialRequest) {
public MaterialRequest recheckPid(MaterialRequest materialRequest) throws NoPidException, EasySMTAException {
// re-test email for PID
final EasySMTA.EasySMTAUserData pid = pidChecker.getUserData(materialRequest.getEmail());
EasySMTA.EasySMTAUserData pid = pidChecker.getUserData(materialRequest.getEmail());
if (pid == null) {
throw new NoPidException("Email not registered with PID server");
}
......@@ -271,11 +277,13 @@ public class RequestServiceImpl implements RequestService {
*
* @param materialRequest
* @return
* @throws EasySMTAException
* @throws NoPidException
*/
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR')")
public MaterialRequest validateRequest(MaterialRequest materialRequest) {
public MaterialRequest validateRequest(MaterialRequest materialRequest) throws NoPidException, EasySMTAException {
// Client email is confirmed
materialRequest.setState(MaterialRequest.VALIDATED);
materialRequest = requestRepository.save(materialRequest);
......@@ -294,8 +302,9 @@ public class RequestServiceImpl implements RequestService {
*
* @param materialRequest
* @return
* @throws NoPidException
*/
private List<MaterialSubRequest> breakup(MaterialRequest materialRequest) {
private List<MaterialSubRequest> breakup(MaterialRequest materialRequest) throws NoPidException {
if (StringUtils.isBlank(materialRequest.getPid())) {
LOG.warn("Material request has no PID, will not break it up.");
throw new NoPidException("Not breaking up request without PID.");
......
......@@ -28,6 +28,7 @@ import org.genesys2.server.service.RequestService;
import org.genesys2.server.service.RequestService.RequestException;
import org.genesys2.server.service.RequestService.RequestInfo;
import org.genesys2.server.service.TokenVerificationService.NoSuchVerificationTokenException;
import org.genesys2.server.service.impl.EasySMTAException;
import org.genesys2.spring.SecurityContextUtil;
import org.genesys2.util.ReCaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -44,7 +45,7 @@ import org.springframework.web.bind.annotation.RequestParam;
/**
* Controller to manage request processing.
*
*
* @author matijaobreza
*/
@Controller
......@@ -72,7 +73,7 @@ public class RequestController extends BaseController {
/**
* Give information about the request process
*
*
* @param model
* @return
*/
......@@ -177,7 +178,10 @@ public class RequestController extends BaseController {
_logger.error(e.getMessage(), e);
} catch (final NoSuchVerificationTokenException e) {
_logger.error("Verification token is not valid");
model.addAttribute("error", e);
return validateClientRequest(model, tokenUuid, null);
} catch (EasySMTAException e) {
_logger.error("Error connecting to EasySMTA: " + e.getMessage());
model.addAttribute("error", e);
return validateClientRequest(model, tokenUuid, null);
}
......@@ -187,7 +191,7 @@ public class RequestController extends BaseController {
/**
* Genebank confirms receipt of request
*
*
* @param model
* @param tokenUuid
* @return
......
......@@ -21,6 +21,8 @@ import org.genesys2.server.model.genesys.MaterialSubRequest;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.RequestService;
import org.genesys2.server.service.RequestService.NoPidException;
import org.genesys2.server.service.impl.EasySMTAException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
......@@ -91,10 +93,12 @@ public class RequestsController extends RestController {
* Validate request
*
* @return
* @throws EasySMTAException
* @throws NoPidException
*/
@RequestMapping(value = "/requests/r/{uuid:.{36}}/validate", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
MaterialRequest validateRequest(@PathVariable("uuid") String uuid) {
MaterialRequest validateRequest(@PathVariable("uuid") String uuid) throws NoPidException, EasySMTAException {
LOG.info("Loading request uuid=" + uuid);
final MaterialRequest materialRequest = requestService.get(uuid);
return requestService.validateRequest(materialRequest);
......@@ -104,10 +108,12 @@ public class RequestsController extends RestController {
* Reload PID data
*
* @return
* @throws EasySMTAException
* @throws NoPidException
*/
@RequestMapping(value = "/requests/r/{uuid:.{36}}/update-pid", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
MaterialRequest updatePid(@PathVariable("uuid") String uuid) {
MaterialRequest updatePid(@PathVariable("uuid") String uuid) throws NoPidException, EasySMTAException {
LOG.info("Loading request uuid=" + uuid);
final MaterialRequest materialRequest = requestService.get(uuid);
return requestService.recheckPid(materialRequest);
......
......@@ -224,7 +224,7 @@ accession.collecting.source=Collecting/Acquisition source
accession.collectingSource.=
accession.collectingSource.10=Wild habitat
accession.collectingSource.11=Forest or woodland
accession.collectingSource.11=Forest or woodland
accession.collectingSource.12=Shrubland
accession.collectingSource.13=Grassland
accession.collectingSource.14=Desert or tundra
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment