Skip to content

Commit

Permalink
parse domain
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Sikina committed Oct 8, 2023
1 parent b3e5448 commit 7c02fd6
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package edu.harvard.dbmi.avillach.data.entity;

import io.swagger.v3.oas.annotations.media.Schema;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Schema(description = "A site that contains a PIC-SURE installation that we can send data to")
@Table(uniqueConstraints = {
@UniqueConstraint(name = "unique_code", columnNames = { "code" }),
@UniqueConstraint(name = "unique_email", columnNames = { "domain" })
})
@Entity(name = "site")
public class Site extends BaseEntity {

@Schema(description = "The site code. Ex: BCH")
@Column(length = 15)
private String code;

@Schema(description = "The site name. Ex: Boston Children's")
@Column(length = 255)
private String name;

@Schema(description = "The email domain of users for this site. Ex: childrens.harvard.edu")
@Column(length = 255)
private String domain;

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDomain() {
return domain;
}

public void setDomain(String domain) {
this.domain = domain;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package edu.harvard.dbmi.avillach.data.repository;

import edu.harvard.dbmi.avillach.data.entity.NamedDataset;
import edu.harvard.dbmi.avillach.data.entity.Site;

import javax.enterprise.context.ApplicationScoped;
import javax.transaction.Transactional;
import java.util.UUID;

@Transactional
@ApplicationScoped
public class SiteRepository extends BaseRepository<Site, UUID>{
protected SiteRepository() {super(Site.class);}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
import javax.ws.rs.core.SecurityContext;

import edu.harvard.dbmi.avillach.domain.*;
import edu.harvard.dbmi.avillach.service.FormatService;
import edu.harvard.dbmi.avillach.service.PicsureInfoService;
import edu.harvard.dbmi.avillach.service.PicsureQueryService;
import edu.harvard.dbmi.avillach.service.PicsureSearchService;
import edu.harvard.dbmi.avillach.service.*;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.info.Info;
Expand Down Expand Up @@ -161,8 +158,7 @@ public QueryStatus query(
if (isInstitutionQuery == null || !isInstitutionQuery) {
return queryService.query(dataQueryRequest, headers);
} else {
dataQueryRequest.setInstitutionOfOrigin(context.getUserPrincipal().getName());
return queryService.institutionalQuery(dataQueryRequest, headers);
return queryService.institutionalQuery(dataQueryRequest, headers, context.getUserPrincipal().getName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public class PicsureQueryService {
@Inject
ResourceWebClient resourceWebClient;

@Inject
SiteParsingService siteParsingService;

/**
* Executes a query on a PIC-SURE resource and creates a Query entity in the
* database for the query.
Expand Down Expand Up @@ -277,7 +280,9 @@ public QueryStatus queryMetadata(UUID queryId, HttpHeaders headers){
* and resource specific query (could be a string or a json object)
* @return {@link QueryStatus}
*/
public QueryStatus institutionalQuery(QueryRequest dataQueryRequest, HttpHeaders headers) {
public QueryStatus institutionalQuery(QueryRequest dataQueryRequest, HttpHeaders headers, String email) {
String siteCode = siteParsingService.parseSiteOfOrigin(email).orElseThrow(() -> new RuntimeException("Bad email"));
dataQueryRequest.setInstitutionOfOrigin(siteCode);
Resource resource = verifyQueryRequest(dataQueryRequest, headers);
dataQueryRequest.getResourceCredentials().put(ResourceWebClient.BEARER_TOKEN_KEY, resource.getToken());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package edu.harvard.dbmi.avillach.service;

import edu.harvard.dbmi.avillach.data.entity.Site;
import edu.harvard.dbmi.avillach.data.repository.SiteRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import java.util.List;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SiteParsingService {

private static final Logger LOG = LoggerFactory.getLogger(SiteParsingService.class);

private static final Pattern emailRegex = Pattern.compile("^([^@]+)(@)(.*)$");

@Inject
SiteRepository repository;

public Optional<String> parseSiteOfOrigin(String email) {
Matcher matcher = emailRegex.matcher(email);
if (!matcher.find()) {
LOG.warn("Unable to parse domain for email: {}", email);
return Optional.empty();
}

List<Site> matchingDomains = repository.getByColumn("domain", matcher.group(3));
if (matchingDomains.isEmpty()) {
LOG.warn("Unable to match domain for email: {}, looked for domain: {}", email, matcher.group(3));
return Optional.empty();
}
if (matchingDomains.size() > 1) {
LOG.warn("Multiple domains match email. This should never happen! Email: {}", email);
return Optional.empty();
}
return Optional.of(matchingDomains.get(0).getCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<class>edu.harvard.dbmi.avillach.data.entity.Query</class>
<class>edu.harvard.dbmi.avillach.data.entity.Resource</class>
<class>edu.harvard.dbmi.avillach.data.entity.NamedDataset</class>
<class>edu.harvard.dbmi.avillach.data.entity.Site</class>

<properties>
<property name="hibernate.archive.autodetection" value="class" />
Expand Down

0 comments on commit 7c02fd6

Please sign in to comment.