Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/difi/vefa-peppol
Browse files Browse the repository at this point in the history
  • Loading branch information
klakegg committed Feb 24, 2018
2 parents a71acb5 + ee86e80 commit d75ea5b
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import no.difi.vefa.peppol.security.api.CertificateValidator;
import no.difi.vefa.peppol.security.lang.PeppolSecurityException;

import java.io.FileNotFoundException;
import java.net.URI;
import java.util.List;

Expand Down Expand Up @@ -56,10 +57,12 @@ public List<DocumentTypeIdentifier> getDocumentIdentifiers(ParticipantIdentifier
URI location = locator.lookup(participantIdentifier);
URI provider = this.provider.resolveDocumentIdentifiers(location, participantIdentifier);

FetcherResponse fetcherResponse = fetcher.fetch(provider);

if (fetcherResponse == null)
throw new LookupException("Receiver not found.");
FetcherResponse fetcherResponse;
try {
fetcherResponse = fetcher.fetch(provider);
} catch (FileNotFoundException e) {
throw new LookupException(String.format("Receiver (%s) not found.", participantIdentifier.toString()), e);
}

return reader.parseDocumentIdentifiers(fetcherResponse);
}
Expand All @@ -70,10 +73,14 @@ public ServiceMetadata getServiceMetadata(ParticipantIdentifier participantIdent
URI location = locator.lookup(participantIdentifier);
URI provider = this.provider.resolveServiceMetadata(location, participantIdentifier, documentTypeIdentifier);

FetcherResponse fetcherResponse = fetcher.fetch(provider);

if (fetcherResponse == null)
throw new LookupException("Combination of receiver and document type identifier not supported.");
FetcherResponse fetcherResponse;
try {
fetcherResponse = fetcher.fetch(provider);
} catch (FileNotFoundException e) {
throw new LookupException(String.format(
"Combination of receiver (%s) and document type identifier (%s) is not supported.",
participantIdentifier.toString(), documentTypeIdentifier.toString()), e);
}

PotentiallySigned<ServiceMetadata> serviceMetadata = reader.parseServiceMetadata(fetcherResponse);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
package no.difi.vefa.peppol.lookup.api;


import java.io.FileNotFoundException;
import java.net.URI;

public interface MetadataFetcher {

FetcherResponse fetch(URI uri) throws LookupException;
FetcherResponse fetch(URI uri) throws LookupException, FileNotFoundException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.apache.http.impl.client.HttpClients;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.URI;
Expand All @@ -50,7 +51,7 @@ public BasicApacheFetcher(Mode mode) {
}

@Override
public FetcherResponse fetch(URI uri) throws LookupException {
public FetcherResponse fetch(URI uri) throws LookupException, FileNotFoundException {
try (CloseableHttpClient httpClient = createClient()) {
HttpGet httpGet = new HttpGet(uri);

Expand All @@ -64,15 +65,15 @@ public FetcherResponse fetch(URI uri) throws LookupException {
response.getFirstHeader("X-SMP-Namespace").getValue() : null
);
case 404:
return null;
throw new FileNotFoundException(uri.toString());
default:
throw new LookupException(String.format(
"Received code %s for lookup. URI: %s", response.getStatusLine().getStatusCode(), uri));
}
}
} catch (SocketTimeoutException | SocketException | UnknownHostException e) {
throw new LookupException(String.format("Unable to fetch '%s'", uri), e);
} catch (LookupException e) {
} catch (LookupException | FileNotFoundException e) {
throw e;
} catch (Exception e) {
throw new LookupException(e.getMessage(), e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public UrlFetcher(Mode mode) {
}

@Override
public FetcherResponse fetch(URI uri) throws LookupException {
public FetcherResponse fetch(URI uri) throws LookupException, FileNotFoundException {
try {
HttpURLConnection urlConnection = (HttpURLConnection) uri.toURL().openConnection();
urlConnection.setConnectTimeout(timeout);
Expand All @@ -49,7 +49,7 @@ public FetcherResponse fetch(URI uri) throws LookupException {
new BufferedInputStream(urlConnection.getInputStream()),
urlConnection.getHeaderField("X-SMP-Namespace"));
} catch (FileNotFoundException e) {
return null;
throw new FileNotFoundException(uri.toString());
} catch (SocketTimeoutException | SocketException e) {
throw new LookupException(String.format("Unable to fetch '%s'", uri), e);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,30 @@
import org.testng.Assert;
import org.testng.annotations.Test;

import java.io.FileNotFoundException;
import java.net.URI;

public class ApacheFetcherTest {

private MetadataFetcher fetcher = new ApacheFetcher(Mode.of("TEST"));

@Test(expectedExceptions = LookupException.class)
public void simpleTimeout() throws LookupException {
public void simpleTimeout() throws LookupException, FileNotFoundException {
fetcher.fetch(URI.create("http://invalid.example.com/"));
}

@Test
public void simple404() throws LookupException {
Assert.assertNull(fetcher.fetch(URI.create("http://httpstat.us/404")));
@Test(expectedExceptions = FileNotFoundException.class)
public void simple404() throws LookupException, FileNotFoundException {
fetcher.fetch(URI.create("http://httpstat.us/404"));
}

@Test(expectedExceptions = LookupException.class)
public void simple500() throws LookupException {
public void simple500() throws LookupException, FileNotFoundException {
fetcher.fetch(URI.create("http://httpstat.us/500"));
}

@Test(expectedExceptions = LookupException.class)
public void simpleNullPointer() throws LookupException {
public void simpleNullPointer() throws LookupException, FileNotFoundException {
fetcher.fetch(null);
}
}

0 comments on commit d75ea5b

Please sign in to comment.