diff --git a/.classpath b/.classpath index f0a6883..590512e 100644 --- a/.classpath +++ b/.classpath @@ -25,5 +25,22 @@ + + + + + + + + + + + + + + + + + diff --git a/.project b/.project index 0d05917..d562265 100644 --- a/.project +++ b/.project @@ -20,4 +20,15 @@ org.eclipse.m2e.core.maven2Nature org.eclipse.jdt.core.javanature + + + 1680091733281 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 2f5cc74..1b6e1ef 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -4,5 +4,6 @@ org.eclipse.jdt.core.compiler.compliance=1.8 org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore +org.eclipse.jdt.core.compiler.processAnnotations=disabled org.eclipse.jdt.core.compiler.release=disabled org.eclipse.jdt.core.compiler.source=1.8 diff --git a/pom.xml b/pom.xml index 8300c4c..9c05103 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 laverca-rest laverca-rest - 1.0.0 + 1.1.0 Laverca REST MSS client @@ -177,4 +177,4 @@ - \ No newline at end of file + diff --git a/src/main/java/fi/methics/laverca/rest/DocxSigner.java b/src/main/java/fi/methics/laverca/rest/DocxSigner.java index 7221819..74399a1 100644 --- a/src/main/java/fi/methics/laverca/rest/DocxSigner.java +++ b/src/main/java/fi/methics/laverca/rest/DocxSigner.java @@ -62,6 +62,10 @@ public ByteArrayOutputStream signDocument(final String msisdn, MssCertificate cert = this.client.getCertificate(msisdn, signatureProfile); + if (cert.getCertificate() == null) { + throw new MssRestException(MssRestException.UNKNOWN_USER, "Failed to get user certificate"); + } + SignatureConfig signatureConfig = new SignatureConfig(); signatureConfig.setSigningCertificateChain(cert.getCertificateChain()); signatureConfig.setIncludeEntireCertificateChain(true); diff --git a/src/main/java/fi/methics/laverca/rest/MssClient.java b/src/main/java/fi/methics/laverca/rest/MssClient.java index 456e51b..ccd0ee0 100644 --- a/src/main/java/fi/methics/laverca/rest/MssClient.java +++ b/src/main/java/fi/methics/laverca/rest/MssClient.java @@ -316,7 +316,8 @@ public MSS_SignatureResp sign(final MSS_SignatureReq req) throws MssRestExceptio */ public static class Builder { - private String resturl; + private String restUrl; + private String secondaryUrl; private String apid; private String apname; private String password; @@ -336,18 +337,29 @@ public MssClient build() { client.client.setApName(this.apname); client.client.setPassword(this.password); } - client.client.setRestUrl(this.resturl); + client.client.setRestUrl(this.restUrl); + client.client.setSecondaryUrl(this.secondaryUrl); client.appwd = this.appwd; return client; } /** - * Set RESTAPI service URL + * Set primary RESTAPI service URL * @param resturl RESTAPI service URL * @return this builder */ public Builder withRestUrl(String resturl) { - this.resturl = resturl; + this.restUrl = resturl; + return this; + } + + /** + * Set alternative RESTAPI service URL + * @param alternateUrl RESTAPI service URL + * @return this builder + */ + public Builder withSecondaryUrl(String alternateUrl) { + this.secondaryUrl = alternateUrl; return this; } diff --git a/src/main/java/fi/methics/laverca/rest/PdfSigner.java b/src/main/java/fi/methics/laverca/rest/PdfSigner.java index c32f825..bac156f 100644 --- a/src/main/java/fi/methics/laverca/rest/PdfSigner.java +++ b/src/main/java/fi/methics/laverca/rest/PdfSigner.java @@ -37,6 +37,7 @@ import fi.methics.laverca.rest.util.DocumentSigner; import fi.methics.laverca.rest.util.LavercaPAdESService; import fi.methics.laverca.rest.util.MssCertificate; +import fi.methics.laverca.rest.util.MssRestException; import fi.methics.laverca.rest.util.SignatureProfile; /** @@ -205,6 +206,11 @@ private void addSignatureField(final String msisdn, final PDAcroForm acroForm, f */ private PAdESSignatureParameters createParams(String msisdn, SignatureProfile sigprof) { MssCertificate cert = this.client.getCertificate(msisdn, sigprof); + + if (cert.getCertificate() == null) { + throw new MssRestException(MssRestException.UNKNOWN_USER, "Failed to get user certificate"); + } + PAdESSignatureParameters parameters = new PAdESSignatureParameters(); parameters.setSignatureLevel(SignatureLevel.PAdES_BASELINE_B); parameters.setSignaturePackaging(SignaturePackaging.ENVELOPED); diff --git a/src/main/java/fi/methics/laverca/rest/util/RestClient.java b/src/main/java/fi/methics/laverca/rest/util/RestClient.java index cbedfde..c96e602 100644 --- a/src/main/java/fi/methics/laverca/rest/util/RestClient.java +++ b/src/main/java/fi/methics/laverca/rest/util/RestClient.java @@ -5,6 +5,7 @@ import java.io.IOException; +import java.net.MalformedURLException; import java.net.URL; import java.util.Base64; @@ -37,6 +38,7 @@ public class RestClient { private HttpClient httpClient; private String resturl; + private String secondaryUrl; private String apid; private String apikey; @@ -71,6 +73,10 @@ public void setRestUrl(final String resturl) { this.resturl = resturl; } + public void setSecondaryUrl(final String resturl) { + this.resturl = resturl; + } + public void setAuthnMode(final AuthnMode mode) { this.mode = mode; } @@ -135,6 +141,15 @@ private String sendHmacReq(final String req) throws MssRestException { return client.send(req, this.resturl); } catch (IOException e) { log.error("Connection to " + this.resturl + " failed: " + e.getMessage()); + try { + if (this.secondaryUrl != null) { + HmacHttpClient client = new HmacHttpClient(this.httpClient, userid, apikey); + return client.send(req, this.secondaryUrl); + } + } catch (IOException e2) { + log.error("Connection to " + this.secondaryUrl + " failed: " + e.getMessage()); + throw new MssRestException(MssRestException.UNABLE_TO_PROVIDE_SERVICES, e2.getMessage()); + } throw new MssRestException(MssRestException.UNABLE_TO_PROVIDE_SERVICES, e.getMessage()); } } @@ -151,22 +166,19 @@ private String sendHmacReq(final String req) throws MssRestException { private String sendBasicReq(final String req) throws MssRestException { try { - CredentialsProvider provider = new BasicCredentialsProvider(); - provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(this.apname, this.password)); - HttpClientContext ctx = HttpClientContext.create(); - - URL url = new URL(this.resturl); - AuthCache authCache = new BasicAuthCache(); - HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); - authCache.put(targetHost, new BasicScheme()); - - ctx.setAuthCache(authCache); - ctx.setCredentialsProvider(provider); - HttpPost post = this.createPost(req, this.resturl); - return this.getResponseBody(this.httpClient.execute(post, ctx)); + return this.getResponseBody(this.httpClient.execute(post, this.createContext(this.resturl))); } catch (IOException e) { - log.error("Connection to " + this.resturl + " failed (TestUtil): " + e.getMessage()); + log.error("Connection to " + this.resturl + " failed: " + e.getMessage()); + if (this.secondaryUrl != null) { + try { + HttpPost post = this.createPost(req, this.secondaryUrl); + return this.getResponseBody(this.httpClient.execute(post, this.createContext(this.secondaryUrl))); + } catch (IOException e2) { + log.error("Connection to " + this.secondaryUrl + " failed: " + e.getMessage()); + throw new MssRestException(MssRestException.UNABLE_TO_PROVIDE_SERVICES, e.getMessage()); + } + } throw new MssRestException(MssRestException.UNABLE_TO_PROVIDE_SERVICES, e.getMessage()); } } @@ -185,6 +197,32 @@ private String getUserId(final String apid) { } } + /** + * Create a HTTP client context for given URL + * @param _url URL + * @return Context + * @throws MssRestException + */ + private HttpClientContext createContext(final String _url) throws MssRestException { + HttpClientContext ctx = HttpClientContext.create(); + try { + CredentialsProvider provider = new BasicCredentialsProvider(); + provider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(this.apname, this.password)); + + URL url = new URL(_url); + AuthCache authCache = new BasicAuthCache(); + HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); + authCache.put(targetHost, new BasicScheme()); + + ctx.setAuthCache(authCache); + ctx.setCredentialsProvider(provider); + } catch (MalformedURLException e) { + log.error("Connection to " + _url + " failed: " + e.getMessage()); + throw new MssRestException(MssRestException.UNABLE_TO_PROVIDE_SERVICES, e.getMessage()); + } + return ctx; + } + /** * Create a HTTP Post * @param req Request as String