Commit 7dfdbe09 authored by Matija Obreza's avatar Matija Obreza
Browse files

Fixed #20

parent c235db33
......@@ -16,6 +16,15 @@
package org.genesys2.server.service;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
public interface EasySMTA {
EasySMTAUserData getUserData(String emailAddress);
......@@ -25,73 +34,325 @@ public interface EasySMTA {
*
* @author matijaobreza
*/
@JsonDeserialize(using = Deserializer.class)
public static class EasySMTAUserData {
private String pid;
private String firstName;
private String lastName;
// / "in" = Individual, "or" = Organization
private String type;
private String legalStatus;
private String name;
private String surname;
private String email;
private String organization;
private String countryIsoCode3;
private String address;
private String country;
private String countryName;
private String telephone;
private String fax;
private String orgName;
private String aoName, aoSurname, aoEmail;
private String orgAddress, orgCountry, orgCountryName;
private String aoTelephone, aoFax;
private String shipAddrFlag;
private String shipAddress;
private String shipCountry;
private String shipTelephone;
public EasySMTAUserData() {
}
public EasySMTAUserData(String pid, String firstName, String lastName, String email, String organization, String countryIsoCode3, String address,
String shipAddress, String shipCountry) {
this.pid = pid;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.organization = organization;
this.countryIsoCode3 = countryIsoCode3;
this.address = address;
this.shipAddress = shipAddress;
this.shipCountry = shipCountry;
}
@Override
public String toString() {
return "ITPGRFA.PID email=" + email + " first=" + firstName + " last=" + lastName + ", org=" + organization + ", country=" + countryIsoCode3;
return "ITPGRFA.PID email=" + email + " first=" + name + " last=" + surname + ", org=" + orgName + ", country=" + country;
}
public String getPid() {
return pid;
}
public String getFirstName() {
return firstName;
public void setPid(String pid) {
this.pid = pid;
}
public String getLastName() {
return lastName;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getLegalStatus() {
return legalStatus;
}
public void setLegalStatus(String legalStatus) {
this.legalStatus = legalStatus;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
public String getEmail() {
return email;
}
public String getOrganization() {
return organization;
}
public String getCountryIsoCode3() {
return countryIsoCode3;
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getFax() {
return fax;
}
public void setFax(String fax) {
this.fax = fax;
}
public String getOrgName() {
return orgName;
}
public void setOrgName(String orgName) {
this.orgName = orgName;
}
public String getAoName() {
return aoName;
}
public void setAoName(String aoName) {
this.aoName = aoName;
}
public String getAoSurname() {
return aoSurname;
}
public void setAoSurname(String aoSurname) {
this.aoSurname = aoSurname;
}
public String getAoEmail() {
return aoEmail;
}
public void setAoEmail(String aoEmail) {
this.aoEmail = aoEmail;
}
public String getOrgAddress() {
return orgAddress;
}
public void setOrgAddress(String orgAddress) {
this.orgAddress = orgAddress;
}
public String getOrgCountry() {
return orgCountry;
}
public void setOrgCountry(String orgCountry) {
this.orgCountry = orgCountry;
}
public String getOrgCountryName() {
return orgCountryName;
}
public void setOrgCountryName(String orgCountryName) {
this.orgCountryName = orgCountryName;
}
public String getAoTelephone() {
return aoTelephone;
}
public void setAoTelephone(String aoTelephone) {
this.aoTelephone = aoTelephone;
}
public String getAoFax() {
return aoFax;
}
public void setAoFax(String aoFax) {
this.aoFax = aoFax;
}
public String getShipAddrFlag() {
return shipAddrFlag;
}
public void setShipAddrFlag(String shipAddrFlag) {
this.shipAddrFlag = shipAddrFlag;
}
public String getShipAddress() {
return shipAddress;
}
public void setShipAddress(String shipAddress) {
this.shipAddress = shipAddress;
}
public String getShipCountry() {
return shipCountry;
}
public void setShipCountry(String shipCountry) {
this.shipCountry = shipCountry;
}
public String getShipTelephone() {
return shipTelephone;
}
public void setShipTelephone(String shipTelephone) {
this.shipTelephone = shipTelephone;
}
}
public final class Deserializer extends StdDeserializer<EasySMTAUserData> {
public Deserializer() {
super(EasySMTAUserData.class);
}
protected Deserializer(Class<?> vc) {
super(vc);
}
@Override
public EasySMTAUserData deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
if (jp.getCurrentToken() != JsonToken.START_OBJECT) {
throw new IOException("invalid start marker");
}
EasySMTAUserData pidData = new EasySMTAUserData();
boolean foundName = false, foundSurname = false;
boolean foundOrgname = false, foundCountry=false;
while (jp.nextToken() != JsonToken.END_OBJECT) {
String fieldname = jp.getCurrentName();
jp.nextToken(); // move to next token in string
if ("pid".equals(fieldname)) {
pidData.setPid(jp.getText());
} else if ("type".equals(fieldname)) {
pidData.setType(jp.getText());
} else if ("legalStatus".equals(fieldname)) {
pidData.setLegalStatus(jp.getText());
} else if ("name".equals(fieldname) || "firstName".equals(fieldname)) {
if (foundName)
throw new JsonMappingException("Field " + fieldname + " is a synonym. Don't provide both.");
foundName = true;
pidData.setName(jp.getText());
} else if ("surname".equals(fieldname) || "lastName".equals(fieldname)) {
if (foundSurname)
throw new JsonMappingException("Field " + fieldname + " is a synonym. Don't provide both.");
foundSurname = true;
pidData.setSurname(jp.getText());
} else if ("email".equals(fieldname)) {
pidData.setEmail(jp.getText());
} else if ("address".equals(fieldname)) {
pidData.setAddress(jp.getText());
} else if ("country".equals(fieldname) || "countryIsoCode3".equals(fieldname)) {
if (foundCountry)
throw new JsonMappingException("Field " + fieldname + " is a synonym. Don't provide both.");
foundCountry = true;
pidData.setCountry(jp.getText());
} else if ("countryName".equals(fieldname)) {
pidData.setCountryName(jp.getText());
} else if ("telephone".equals(fieldname)) {
pidData.setTelephone(jp.getText());
} else if ("fax".equals(fieldname)) {
pidData.setFax(jp.getText());
} else if ("orgName".equals(fieldname) || "organization".equals(fieldname)) {
if (foundOrgname)
throw new JsonMappingException("Field " + fieldname + " is a synonym. Don't provide both.");
foundOrgname = true;
pidData.setOrgName(jp.getText());
} else if ("orgAddress".equals(fieldname)) {
pidData.setOrgAddress(jp.getText());
} else if ("orgCountry".equals(fieldname)) {
pidData.setOrgCountry(jp.getText());
} else if ("orgCountryName".equals(fieldname)) {
pidData.setOrgCountryName(jp.getText());
} else if ("aoName".equals(fieldname)) {
pidData.setAoName(jp.getText());
} else if ("aoSurname".equals(fieldname)) {
pidData.setAoSurname(jp.getText());
} else if ("aoEmail".equals(fieldname)) {
pidData.setAoEmail(jp.getText());
} else if ("aoTelephone".equals(fieldname)) {
pidData.setAoTelephone(jp.getText());
} else if ("aoFax".equals(fieldname)) {
pidData.setAoFax(jp.getText());
} else if ("shipAddrFlag".equals(fieldname)) {
pidData.setShipAddrFlag(jp.getText());
} else if ("shipAddress".equals(fieldname)) {
pidData.setShipAddress(jp.getText());
} else if ("shipCountry".equals(fieldname)) {
pidData.setShipCountry(jp.getText());
} else if ("shipTelephone".equals(fieldname)) {
pidData.setShipTelephone(jp.getText());
}
else {
throw new JsonMappingException("Unsupported EasySMTAUserData field: " + fieldname);
}
}
// Don't close?!
//jp.close();
return pidData;
}
}
}
......@@ -100,10 +100,7 @@ public class EasySMTAConnector implements EasySMTA {
// TODO Report various errorCodes!
} else {
return new EasySMTA.EasySMTAUserData(tree.get("pid").asText(), tree.get("name").asText(), tree.get("surname").asText(), tree.get("email").asText(),
tree.get("orgName").asText(), tree.get("country").asText(), tree.get("address").asText(), tree.get("shipAddress").asText(), tree
.get("shipCountry").asText());
return objectMapper.readValue(entity.getContent(), EasySMTA.EasySMTAUserData.class);
}
}
} catch (final ClientProtocolException e) {
......
......@@ -36,8 +36,19 @@ public class EasySMTAMockConnector implements EasySMTA {
if (RandomUtils.nextBoolean()) {
LOG.info("Valid mock EasySMTA check");
return new EasySMTA.EasySMTAUserData(RandomStringUtils.randomAlphanumeric(6), "Name", "Surname", emailAddress, "Organization", "DEU", "Address 1",
"Address 2", "DEU");
EasySMTAUserData pidData = new EasySMTA.EasySMTAUserData();
pidData.setPid(RandomStringUtils.randomAlphanumeric(6));
pidData.setType("in");
pidData.setTelephone("+00 000 000-0000");
pidData.setName("Name");
pidData.setSurname("Surname");
pidData.setEmail(emailAddress);
pidData.setOrgName("Organization");
pidData.setCountry("DEU");
pidData.setAddress("Address 1");
pidData.setShipAddress("Address 2");
pidData.setShipCountry("DEU");
return pidData;
} else {
LOG.info("Invalid mock EasySMTA check");
return null;
......
......@@ -236,25 +236,21 @@ public class RequestServiceImpl implements RequestService {
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR')")
public MaterialRequest recheckPid(MaterialRequest materialRequest) {
if (StringUtils.isBlank(materialRequest.getPid())) {
// re-test email for PID
EasySMTA.EasySMTAUserData pid = pidChecker.getUserData(materialRequest.getEmail());
if (pid == null) {
throw new NoPidException("Email not registered with PID server");
}
materialRequest.setPid(pid.getPid());
requestRepository.save(materialRequest);
try {
RequestBody rb = mapper.readValue(materialRequest.getBody(), RequestBody.class);
rb.pid = pid;
materialRequest.setBody(serialize(rb));
return requestRepository.save(materialRequest);
} catch (IOException e) {
// FIXME Some other exception?
throw new RuntimeException("Could not handle request JSON", e);
}
} else {
return materialRequest;
// re-test email for PID
EasySMTA.EasySMTAUserData pid = pidChecker.getUserData(materialRequest.getEmail());
if (pid == null) {
throw new NoPidException("Email not registered with PID server");
}
materialRequest.setPid(pid.getPid());
requestRepository.save(materialRequest);
try {
RequestBody rb = mapper.readValue(materialRequest.getBody(), RequestBody.class);
rb.pid = pid;
materialRequest.setBody(serialize(rb));
return requestRepository.save(materialRequest);
} catch (IOException e) {
// FIXME Some other exception?
throw new RuntimeException("Could not handle request JSON", e);
}
}
......@@ -406,6 +402,7 @@ public class RequestServiceImpl implements RequestService {
}
public static final class RequestBody {
public RequestInfo requestInfo;
public Set<Long> accessionIds;
public EasySMTA.EasySMTAUserData pid;
......
......@@ -99,6 +99,20 @@ public class RequestsController extends RestController {
MaterialRequest materialRequest = requestService.get(uuid);
return requestService.validateRequest(materialRequest);
}
/**
* Reload PID data
*
* @return
*/
@RequestMapping(value = "/requests/r/{uuid:.{36}}/update-pid", method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody
MaterialRequest updatePid(@PathVariable("uuid") String uuid) {
LOG.info("Loading request uuid=" + uuid);
MaterialRequest materialRequest = requestService.get(uuid);
return requestService.recheckPid(materialRequest);
}
/**
* List institute requests
......
......@@ -45,6 +45,7 @@ public abstract class RestController {
@ResponseBody
public ExceptionJson handleIOException(Exception ex, HttpServletRequest request, HttpServletResponse response) throws IOException {
LOG.error(ex, ex);
response.setStatus(400); // ?
return new ExceptionJson(ex);
}
......
package org.genesys2.server.test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import org.genesys2.server.service.EasySMTA;
import org.genesys2.server.service.EasySMTA.EasySMTAUserData;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class EasySMTATest {
@Test
public void testSelf() throws JsonParseException, JsonMappingException, IOException {
ObjectMapper mapper = new ObjectMapper();
EasySMTAUserData pid1=new EasySMTAUserData();
pid1.setPid("pid");
String content = mapper.writeValueAsString(pid1);
EasySMTAUserData pidData = mapper.readValue(content, EasySMTA.EasySMTAUserData.class);
assertTrue(pidData != null);
}
@Test
public void testJSONRead() {
String content = "{\"pid\":\"000000\",\"type\":\"in\",\"legalStatus\":\"cg\",\"name\":\"Matija\",\"surname\":\"Obreza\",\"email\":\"matija.obreza@croptrust.org\",\"address\":\"Platz der Vereinten Nationen 7\\r\\n53113 Bonn\",\"country\":\"DEU\",\"countryName\":\"Germany\",\"telephone\":\"\",\"fax\":\"\",\"orgName\":\"\",\"aoName\":\"\",\"aoSurname\":\"\",\"aoEmail\":\"\",\"orgAddress\":\"\",\"orgCountry\":\"\",\"orgCountryName\":\"\",\"aoTelephone\":\"\",\"aoFax\":\"\",\"shipAddrFlag\":\"s\",\"shipAddress\":\"\",\"shipCountry\":\"AFG\",\"shipTelephone\":\"\"}";
ObjectMapper mapper = new ObjectMapper();
try {
EasySMTAUserData pidData = mapper.readValue(content, EasySMTA.EasySMTAUserData.class);
assertTrue(pidData != null);
} catch (IOException e) {
fail(e.getMessage());
}
}
@Test
public void testJSONReadMissing() {
String content = "{\"pid\":\"000000\",\"firstName\":\"Matija\",\"lastName\":\"Obreza\",\"email\":\"matija.obreza@croptrust.org\",\"address\":\"Platz der Vereinten Nationen 7\\r\\n53113 Bonn\",\"country\":\"DEU\",\"countryName\":\"Germany\",\"telephone\":\"\",\"fax\":\"\",\"orgName\":\"\",\"aoName\":\"\",\"aoSurname\":\"\",\"aoEmail\":\"\",\"orgAddress\":\"\",\"orgCountry\":\"\",\"orgCountryName\":\"\",\"aoTelephone\":\"\",\"aoFax\":\"\",\"shipAddrFlag\":\"s\",\"shipAddress\":\"\",\"shipCountry\":\"AFG\",\"shipTelephone\":\"\"}";
ObjectMapper mapper = new ObjectMapper();
try {
EasySMTAUserData pidData = mapper.readValue(content, EasySMTA.EasySMTAUserData.class);
assertTrue(pidData != null);
} catch (IOException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
@Test(expected = JsonMappingException.class)
public void testJSONSurnameLastname() throws JsonParseException, JsonMappingException, IOException {
String content = "{\"pid\":\"000000\",\"firstName\":\"Matija\",\"lastName\":\"Obreza\",\"surname\":\"Obreza\",\"email\":\"matija.obreza@croptrust.org\",\"address\":\"Platz der Vereinten Nationen 7\\r\\n53113 Bonn\",\"country\":\"DEU\",\"countryName\":\"Germany\",\"telephone\":\"\",\"fax\":\"\",\"orgName\":\"\",\"aoName\":\"\",\"aoSurname\":\"\",\"aoEmail\":\"\",\"orgAddress\":\"\",\"orgCountry\":\"\",\"orgCountryName\":\"\",\"aoTelephone\":\"\",\"aoFax\":\"\",\"shipAddrFlag\":\"s\",\"shipAddress\":\"\",\"shipCountry\":\"AFG\",\"shipTelephone\":\"\"}";
ObjectMapper mapper = new ObjectMapper();
EasySMTAUserData pidData = mapper.readValue(content, EasySMTA.EasySMTAUserData.class);
assertTrue(pidData != null);
}
@Test
public void testJSONNull() throws JsonParseException, JsonMappingException, IOException {
String content = "null";
ObjectMapper mapper = new ObjectMapper();
EasySMTAUserData pidData = mapper.readValue(content, EasySMTA.EasySMTAUserData.class);
assertTrue(pidData == null);
}
}
......@@ -181,12 +181,6 @@ public class MaterialRequestTest {
assertTrue("Got something else back", loaded.getId().equals(request.getId()));
}
EasySMTA.EasySMTAUserData generatePid() {
EasySMTA.EasySMTAUserData pid = new EasySMTA.EasySMTAUserData("pid1", "firstName", "lastName", "email@localhost", "organization", "DEU", "address",
"shipAddress", "AFG");
return pid;
}
Set<Long> generateAccessionIds() {
Set<Long> accessionIds = new HashSet<Long>();
for (long i = 2 + RandomUtils.nextInt(20); i >= 0; i--) {
......
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