Commit 1228d63c authored by Piotr Gawron's avatar Piotr Gawron
Browse files

add ing ivalid url rejects with proper error

parent ca5b48de
Pipeline #58289 passed with stage
in 12 minutes and 53 seconds
......@@ -3,6 +3,8 @@ minerva (16.2.3) stable; urgency=medium
CellDesigner
* Bug fix: expot to SBML did not work for cmaps that had two or more complexes
with the same children that had modification residues (#1720)
* Bug fix: when providing invalid url in gene mapping proper error message is
shown (#1723)
-- Piotr Gawron <piotr.gawron@uni.lu> Wed, 22 Jun 2022 12:00:00 +0200
......
package lcsb.mapviewer.api.genomics;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
......@@ -13,6 +15,7 @@ import javax.validation.constraints.NotNull;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
......@@ -230,6 +233,19 @@ public class ReferenceGenomeController extends BaseController {
if (!url.toLowerCase().endsWith("bb")) {
throw new QueryException("Only big bed format files are supported but found: \"" + url + "\".");
}
if (url.startsWith("http")) {
try {
HttpURLConnection huc = (HttpURLConnection) new URL(url).openConnection();
huc.setRequestMethod("HEAD");
HttpStatus status = HttpStatus.valueOf(huc.getResponseCode());
if (status != HttpStatus.OK) {
throw new QueryException("Invalid url. Server returned: " + status.name() + " (" + status.value() + ").");
}
} catch (IOException e) {
throw new QueryException("Problem with processing url.", e);
}
}
referenceGenomeService.addReferenceGenomeGeneMapping(genome, name, url);
return new HashMap<>();
......
......@@ -481,4 +481,44 @@ public class GenomicsControllerIntegrationTest extends ControllerIntegrationTest
.andReturn().getResponse().getContentAsString();
}
@Test
public void addNotExistingUrlGeneMapping() throws Exception {
MockHttpSession session = createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD);
ReferenceGenome genome = createGenome();
String body = EntityUtils.toString(new UrlEncodedFormEntity(
Arrays.asList(
new BasicNameValuePair("name", "mappingName"),
new BasicNameValuePair("url", "https://minerva-dev.lcsb.uni.lu/not.existing.bb"))));
RequestBuilder request = post("/api/genomics/{genomeId}/geneMapping/", genome.getId())
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(session);
mockMvc.perform(request)
.andExpect(status().isBadRequest());
}
@Test
public void addInvalidUrlGeneMapping() throws Exception {
MockHttpSession session = createSession(BUILT_IN_TEST_ADMIN_LOGIN, BUILT_IN_TEST_ADMIN_PASSWORD);
ReferenceGenome genome = createGenome();
String body = EntityUtils.toString(new UrlEncodedFormEntity(
Arrays.asList(
new BasicNameValuePair("name", "mappingName"),
new BasicNameValuePair("url", "https://asdasdsa.bb"))));
RequestBuilder request = post("/api/genomics/{genomeId}/geneMapping/", genome.getId())
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.content(body)
.session(session);
mockMvc.perform(request)
.andExpect(status().isBadRequest());
}
}
Supports Markdown
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