diff options
author | Thomas Lenz <thomas.lenz@egiz.gv.at> | 2019-07-19 10:35:29 +0200 |
---|---|---|
committer | Thomas Lenz <thomas.lenz@egiz.gv.at> | 2019-07-19 10:35:29 +0200 |
commit | 2c3c1d6bb748a0655a83fdf4cd26135042d898b9 (patch) | |
tree | 6feabd6d3a6a3aed7751154053b3487de4959d6d /src/main/java/at/gv/util/client | |
parent | 2d03e741a91f6155dce75952b0174e3e52a12740 (diff) | |
download | egovutils-2c3c1d6bb748a0655a83fdf4cd26135042d898b9.tar.gz egovutils-2c3c1d6bb748a0655a83fdf4cd26135042d898b9.tar.bz2 egovutils-2c3c1d6bb748a0655a83fdf4cd26135042d898b9.zip |
USP client integration2.0.7
switch to v2.0.7
Diffstat (limited to 'src/main/java/at/gv/util/client')
-rw-r--r-- | src/main/java/at/gv/util/client/mis/usp_v2/USPClient.java | 171 | ||||
-rw-r--r-- | src/main/java/at/gv/util/client/mis/usp_v2/USPClientException.java | 14 | ||||
-rw-r--r-- | src/main/java/at/gv/util/client/mis/usp_v2/VDDSRoleGenerationStrategy.java | 83 | ||||
-rw-r--r-- | src/main/java/at/gv/util/client/szr/IRoleGenerationStrategy.java | 15 | ||||
-rw-r--r-- | src/main/java/at/gv/util/client/szr/PVP2XHTTPHeaderHandler.java (renamed from src/main/java/at/gv/util/client/szr/SZRHTTPHeaderHandler.java) | 68 | ||||
-rw-r--r-- | src/main/java/at/gv/util/client/szr/SZRClient.java | 4 |
6 files changed, 281 insertions, 74 deletions
diff --git a/src/main/java/at/gv/util/client/mis/usp_v2/USPClient.java b/src/main/java/at/gv/util/client/mis/usp_v2/USPClient.java index 4d23a1e..7bbdef9 100644 --- a/src/main/java/at/gv/util/client/mis/usp_v2/USPClient.java +++ b/src/main/java/at/gv/util/client/mis/usp_v2/USPClient.java @@ -1,7 +1,9 @@ package at.gv.util.client.mis.usp_v2; import java.net.URL; +import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Date; import java.util.List; import java.util.Map; @@ -23,7 +25,7 @@ import org.slf4j.LoggerFactory; import at.gv.util.LaxHostNameVerifier; import at.gv.util.LoggingHandler; import at.gv.util.MiscUtil; -import at.gv.util.client.szr.SZRHTTPHeaderHandler; +import at.gv.util.client.szr.PVP2XHTTPHeaderHandler; import at.gv.util.config.EgovUtilConfiguration; import at.gv.util.ex.EgovUtilException; import at.gv.util.wsdl.mis.usp_v2.ISyncService; @@ -32,18 +34,31 @@ import at.gv.util.wsdl.mis.usp_v2.SynchronisationServiceServiceagent; import at.gv.util.xsd.mis.usp_v2.eai.syncmsgs.ListMandatesRequest; import at.gv.util.xsd.mis.usp_v2.eai.syncmsgs.ListMandatesResponseType; import at.gv.util.xsd.mis.usp_v2.eai.syncmsgs.MandateAttributesSearchFilterType; +import at.gv.util.xsd.mis.usp_v2.eai.syncmsgs.MandateList; +import at.gv.util.xsd.mis.usp_v2.eai.syncmsgs.MandateListType; +import at.gv.util.xsd.mis.usp_v2.eai.syncmsgs.ObjectFactory; +import at.gv.util.xsd.mis.usp_v2.eai.syncmsgs.SearchCriteriaTestType; +import at.gv.util.xsd.mis.usp_v2.eai.syncmsgs.SearchCriteriaType; +import at.gv.util.xsd.mis.usp_v2.eai.syncmsgs.TimeRangeType; +import at.gv.util.xsd.mis.usp_v2.mandates.Mandate; +import at.gv.util.xsd.mis.usp_v2.persondata.IdentificationType; +import at.gv.util.xsd.mis.usp_v2.persondata.IdentificationType.Value; import at.gv.util.xsd.szr.pvp.PvpTokenType; public class USPClient { - @Resource - WebServiceContext wsContext; + public static final String USP_DATE_PATTERN = "yyyy-MM-dd"; + + + @Resource WebServiceContext wsContext; private EgovUtilConfiguration config = null; private Logger log = LoggerFactory.getLogger(USPClient.class); private boolean logEnabled = true; private ISyncService uspSuche = null; + private SimpleDateFormat formater; + public USPClient(EgovUtilConfiguration config, boolean logEnabled) throws EgovUtilException { MiscUtil.assertNotNull(config, "config"); this.config = config; @@ -51,65 +66,100 @@ public class USPClient { initialize(); } -// public GetMandatesResponse getMandates(String bpkType, String bpkValue, List<String> mandateFilters) { -// -// // assemble request -// GetMandatesRequest request = new GetMandatesRequest(); -// String requestId = createUSPRequestId(); -// log.debug("USP request id: " + requestId); -// request.setRequestId(requestId); -// Date date = new Date(); -// try { -// request.setTimestamp(MiscUtil.getXMLGregorianCalendar(date)); -// } catch (DatatypeConfigurationException e) { -// log.warn("Error initializing USP client.", e); -// return null; -// } -// log.debug("USP time stamp: " + date.toString()); -// request.setApplicationId(config.getUSPApplicationId()); -// log.debug("USP application id: " + config.getUSPApplicationId()); -// -// // set identification -// IdentificationType idt = new IdentificationType(); -// idt.setType(bpkType); -// idt.setValue(bpkValue); -// request.setIdentification(idt); -// -// // set filters -// if (mandateFilters != null && mandateFilters.size() > 0) { -// MandateFilters mf = new MandateFilters(); -// mf.getMandateFilter().addAll(mandateFilters); -// request.setMandateFilters(mf); -// } -// -// return this.uspSuche.getMandatesOperation(request); -// -// -// } - - public void getMandates(String bpkType, String bpkValue, List<String> mandateFilters) { - ListMandatesRequest parameters = new ListMandatesRequest(); - - MandateAttributesSearchFilterType searchFilterType = new MandateAttributesSearchFilterType(); - - Object searchFilter = null; - searchFilterType.setFilter(searchFilter ); - - + public List<Mandate> getMandates(String bpkType, String bpkValue, List<String> mandateFilters) throws Exception { - //request USP try { - log.trace("Request pre-processing finished. Requesting USP ... "); - - ListMandatesResponseType results = this.uspSuche.listMandates(parameters ); + if (mandateFilters.size() > 1) + log.debug("More than one mandate-profile required. Request each profile seperatly"); + + List<Mandate> results = new ArrayList<Mandate>(); + for (String mandateFilter : mandateFilters) { + log.trace("Building USP VDDS request ... "); + ListMandatesRequest req = generateRequest(bpkType, bpkValue, mandateFilter); - log.trace("Receive response from USP. Starting response post-processing ... "); + log.trace("Request pre-processing finished. Requesting USP ... "); + ListMandatesResponseType resp = this.uspSuche.listMandates(req ); + + log.trace("Receive response from USP for filter:" + mandateFilter + " . Start post processing ... "); + postProcessUSPMandateResult(resp, results); + + } + return results; + } catch (ListMandatesFault e) { - // TODO Auto-generated catch block - e.printStackTrace(); + log.warn("Receive an error from USP. Msg: " + e.getMessage(), e); + throw e; + + } catch (Exception e) { + log.error("Catch an generic error during USP communication.", e); + throw e; + } + + } + + private void postProcessUSPMandateResult(ListMandatesResponseType resp, List<Mandate> results) throws USPClientException { + if (resp.getSearchResult() != null) { + if (resp.getSearchResult().getDeclaredType().isAssignableFrom(MandateList.class)) { + log.trace("Find 'MandateList' element in USP response"); + MandateList mandateList = (MandateList) resp.getSearchResult().getValue(); + + if (mandateList.getMandates() != null && mandateList.getMandates().getMandate() != null) { + log.debug("Find #" + mandateList.getMandates().getMandate().size() + " mandates in USP response"); + results.addAll(mandateList.getMandates().getMandate()); + + } else + log.info("Find 'null' element in USP response. Ignore full response"); + } else + log.warn("'SearchResult' element in USP response contains an unknown type: " + resp.getSearchResult().getName().toString()); + + } else { + log.debug("'SearchResult' element in USP response is 'null' Inspecting response on errors ..."); + if (resp.getResult() != null) { + String errorCode = resp.getResult().getErrorCode(); + String errorMsg = resp.getResult().getErrorMessage(); + if (MiscUtil.isNotEmpty(errorCode) || MiscUtil.isNotEmpty(errorMsg)) { + log.info("Receice an error from USP! Code: " + errorCode + " Msg: " + errorMsg); + throw new USPClientException(errorMsg, errorCode); + } + + } + } + + } + + private ListMandatesRequest generateRequest(String bpkType, String bpkValue, String mandateFilter) { + ObjectFactory objFactory = new ObjectFactory(); + + ListMandatesRequest parameters = new ListMandatesRequest(); + MandateAttributesSearchFilterType searchFilterType = new MandateAttributesSearchFilterType(); + SearchCriteriaTestType searchCriteriaTest = new SearchCriteriaTestType(); + SearchCriteriaType searchCriteria = new SearchCriteriaType(); + IdentificationType representative = new IdentificationType(); + Value IdValue = new Value(); + + //set bPK + IdValue.setValue(bpkValue); + representative.setValue(IdValue ); + representative.setType(bpkType); + searchCriteria.getActivationRangeOrMandateTypeOrMandatorID().add(objFactory.createSearchCriteriaTypeRepresentativeID(representative)); + + //set allowed time range + TimeRangeType fromTo = new TimeRangeType(); + fromTo.setFrom(formater.format(new Date())); + fromTo.setTo("9999-12-31"); + searchCriteria.getActivationRangeOrMandateTypeOrMandatorID().add(objFactory.createSearchCriteriaTypeValidFromTo(fromTo)); + + //set mandate filters + searchCriteria.getActivationRangeOrMandateTypeOrMandatorID().add(objFactory.createSearchCriteriaTypeMandateType(mandateFilter)); + + searchCriteriaTest.setSearchCriteria(searchCriteria); + searchFilterType.setFilter(searchCriteriaTest); + parameters.getSearchFilter().add(objFactory.createMandateAttributesSearchFilter(searchFilterType)); + + return parameters; } @SuppressWarnings({ "rawtypes", "unchecked" }) @@ -141,7 +191,13 @@ public class USPClient { // PV authentication log.trace("Adding WS-Security Header handler."); PvpTokenType pvpToken = config.getUSPPVPToken(); - SZRHTTPHeaderHandler szrSOAPHandler = new SZRHTTPHeaderHandler(); + + VDDSRoleGenerationStrategy roleStrategy = new VDDSRoleGenerationStrategy( + config.getConfigurationValue("egovutil.usp.role.dynamic.participant"), + config.getConfigurationValue("egovutil.usp.role.dynamic.secret")); + + + PVP2XHTTPHeaderHandler szrSOAPHandler = new PVP2XHTTPHeaderHandler(roleStrategy); szrSOAPHandler.configure(pvpToken); handlerList.add(szrSOAPHandler); @@ -183,6 +239,9 @@ public class USPClient { } http.setTlsClientParameters(tlsParams ); } + + formater = new SimpleDateFormat(USP_DATE_PATTERN); + } private String createUSPRequestId() { diff --git a/src/main/java/at/gv/util/client/mis/usp_v2/USPClientException.java b/src/main/java/at/gv/util/client/mis/usp_v2/USPClientException.java index 96ad965..4b2302b 100644 --- a/src/main/java/at/gv/util/client/mis/usp_v2/USPClientException.java +++ b/src/main/java/at/gv/util/client/mis/usp_v2/USPClientException.java @@ -4,29 +4,29 @@ public class USPClientException extends Exception { private static final long serialVersionUID = 5173056345209288701L; - private int code = -1; + private String code; - public USPClientException(int code) { + public USPClientException(String code) { super(); this.code = code; } - public USPClientException(String message, int code, Throwable cause) { + public USPClientException(String message, String code, Throwable cause) { super(message, cause); this.code = code; } - public USPClientException(String message, int code) { + public USPClientException(String message, String errorCode) { super(message); - this.code = code; + this.code = errorCode; } - public USPClientException(int code, Throwable cause) { + public USPClientException(String code, Throwable cause) { super(cause); this.code = code; } - public int getCode() { + public String getCode() { return this.code; } diff --git a/src/main/java/at/gv/util/client/mis/usp_v2/VDDSRoleGenerationStrategy.java b/src/main/java/at/gv/util/client/mis/usp_v2/VDDSRoleGenerationStrategy.java new file mode 100644 index 0000000..c5573d3 --- /dev/null +++ b/src/main/java/at/gv/util/client/mis/usp_v2/VDDSRoleGenerationStrategy.java @@ -0,0 +1,83 @@ +package at.gv.util.client.mis.usp_v2; + +import java.io.UnsupportedEncodingException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.text.SimpleDateFormat; +import java.util.Base64; +import java.util.Date; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.util.MiscUtil; +import at.gv.util.client.szr.IRoleGenerationStrategy; +import at.gv.util.ex.EgovUtilException; + +public class VDDSRoleGenerationStrategy implements IRoleGenerationStrategy { + private static final Logger log = LoggerFactory.getLogger(VDDSRoleGenerationStrategy.class); + + public static final String ROLE_CONSTANT_PARTNERNAME = "partner-name="; + public static final String ROLE_CONSTANT_SECRET = "secret="; + public static final String ROLE_CONSTANT_CREATIONTS = "creation-ts="; + + public static final String TS_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX"; + private static final SimpleDateFormat tsFormat = new SimpleDateFormat(TS_PATTERN); + + private String participant; + private String authData; + + public VDDSRoleGenerationStrategy(String participant, String authData) throws EgovUtilException { + if (MiscUtil.isEmpty(authData)) { + log.error("Secret to generate VDDS role is 'null or 'empty''"); + throw new EgovUtilException("Secret to generate VDDS role is 'null or 'empty'"); + + } + this.authData = authData; + + if (MiscUtil.isEmpty(participant)) { + log.error("Participant to generate VDDS role is 'null or 'empty''"); + throw new EgovUtilException("Secret to generate VDDS role is 'null or 'empty'"); + + } + this.participant = participant; + + log.trace("VDDSRoleGenerationStrategy uses participant: " + this.participant + " and secret: " + this.authData); + + } + + @Override + public String generateRoleString(String roleString) throws EgovUtilException { + + try { + String currentTS = tsFormat.format(new Date()); + log.trace("Generate VDDS role at: " + currentTS); + MessageDigest md = MessageDigest.getInstance("SHA-1"); + byte[] digistInput = (currentTS + authData).getBytes("UTF-8"); + log.trace("DigistInput: " + new String(digistInput, "UTF-8")); + byte[] digistOutput = md.digest(digistInput); + String secret = Base64.getEncoder().encodeToString(digistOutput); + + StringBuilder sBuilder = new StringBuilder(roleString); + sBuilder.append("("); + sBuilder.append(ROLE_CONSTANT_PARTNERNAME); + sBuilder.append(participant); + sBuilder.append(","); + sBuilder.append(ROLE_CONSTANT_SECRET); + sBuilder.append(secret); + sBuilder.append(","); + sBuilder.append(ROLE_CONSTANT_CREATIONTS); + sBuilder.append(currentTS); + sBuilder.append(")"); + + log.trace("Finale VDDS Role: " + sBuilder.toString()); + return sBuilder.toString(); + + } catch(NoSuchAlgorithmException | UnsupportedEncodingException e) { + log.error("VDDSRoleGeneration FAILED with error: " + e.getMessage(), e); + throw new EgovUtilException("VDDSRoleGeneration FAILED", e); + + } + } + +} diff --git a/src/main/java/at/gv/util/client/szr/IRoleGenerationStrategy.java b/src/main/java/at/gv/util/client/szr/IRoleGenerationStrategy.java new file mode 100644 index 0000000..d411f21 --- /dev/null +++ b/src/main/java/at/gv/util/client/szr/IRoleGenerationStrategy.java @@ -0,0 +1,15 @@ +package at.gv.util.client.szr; + +import at.gv.util.ex.EgovUtilException; + +public interface IRoleGenerationStrategy { + + /** + * Generates a request specific role string + * @param roleString Role from configuration + * + * @return Dynamic generated role + * @throws EgovUtilException + */ + public String generateRoleString(String roleString) throws EgovUtilException; +} diff --git a/src/main/java/at/gv/util/client/szr/SZRHTTPHeaderHandler.java b/src/main/java/at/gv/util/client/szr/PVP2XHTTPHeaderHandler.java index bde6b7b..f4fc11b 100644 --- a/src/main/java/at/gv/util/client/szr/SZRHTTPHeaderHandler.java +++ b/src/main/java/at/gv/util/client/szr/PVP2XHTTPHeaderHandler.java @@ -15,22 +15,35 @@ import org.apache.cxf.message.Message; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import at.gv.util.MiscUtil; +import at.gv.util.ex.EgovUtilException; import at.gv.util.xsd.szr.pvp.Param; import at.gv.util.xsd.szr.pvp.PvpTokenType; import at.gv.util.xsd.szr.pvp.PvpTokenType.Accounting; import at.gv.util.xsd.szr.pvp.PvpTokenType.Accounting.GvCostCenterId; import at.gv.util.xsd.szr.pvp.Role; -public class SZRHTTPHeaderHandler implements SOAPHandler<SOAPMessageContext> { +public class PVP2XHTTPHeaderHandler implements SOAPHandler<SOAPMessageContext> { private PvpTokenType pvpTokenType = null; - private Logger log = LoggerFactory.getLogger(SZRHTTPHeaderHandler.class); + private Logger log = LoggerFactory.getLogger(PVP2XHTTPHeaderHandler.class); public static final String PVP_HEADER_COSTCENTERID_DEFAULT = "<default>"; public static final String PVP_HEADER_VALUE_DELIMITER = ","; public static final String PVP_HEADER_ROLE_VALUE_DELIMITER = ";"; + private IRoleGenerationStrategy roleStrategy = null; + + public PVP2XHTTPHeaderHandler() { + + } + + public PVP2XHTTPHeaderHandler(IRoleGenerationStrategy roleGenerationStrategy) { + log.info("PVP2XHTTPHeaderHandler uses RoleGenerationStrategy: " + roleGenerationStrategy.getClass().getName()); + this.roleStrategy = roleGenerationStrategy; + + } + public void close(MessageContext arg0) { } @@ -58,7 +71,24 @@ public class SZRHTTPHeaderHandler implements SOAPHandler<SOAPMessageContext> { return true; } Map<String, List> headers = (Map<String, List>) smc.get(Message.PROTOCOL_HEADERS); - setHeader(headers, "X-VERSION", pvpTokenType.getVersion()); + //PVP 2.x header + //setHeader(headers, "X-PVP-VERSION", pvpTokenType.getVersion()); + setHeader(headers, "X-PVP-EGOVTOKEN-VERSION", pvpTokenType.getVersion()); + setHeader(headers, "X-PVP-USERID", pvpTokenType.getAuthenticate().getUserPrincipal().getUserId()); + setHeader(headers, "X-PVP-GID", pvpTokenType.getAuthenticate().getUserPrincipal().getGvGid()); + setHeader(headers, "X-PVP-PARTICIPANT-ID", pvpTokenType.getAuthenticate().getParticipantId()); + setHeader(headers, "X-PVP-OU-GV-OU-ID", pvpTokenType.getAuthenticate().getUserPrincipal().getGvOuId()); + setHeader(headers, "X-PVP-OU", pvpTokenType.getAuthenticate().getUserPrincipal().getOu()); + setHeader(headers, "X-PVP-FUNCTION", pvpTokenType.getAuthenticate().getUserPrincipal().getGvFunction()); + setHeader(headers, "X-PVP-SECCLASS", String.valueOf(pvpTokenType.getAuthenticate().getUserPrincipal().getGvSecClass().intValue())); + setHeader(headers, "X-PVP-PRINCIPAL-NAME", pvpTokenType.getAuthenticate().getUserPrincipal().getCn()); + setHeader(headers, "X-PVP-BINDING", "http"); + setHeader(headers, "X-PVP-OU-OKZ", "AT:OVS"); + + + //PVP 1.x header + //setHeader(headers, "X-VERSION", pvpTokenType.getVersion()); + setHeader(headers, "X-VERSION", "1.8"); setHeader(headers, "X-AUTHENTICATE-UserID", pvpTokenType.getAuthenticate().getUserPrincipal().getUserId()); setHeader(headers, "X-AUTHENTICATE-GVGID", pvpTokenType.getAuthenticate().getUserPrincipal().getGvGid()); setHeader(headers, "X-AUTHENTICATE-PARTICIPANTID", pvpTokenType.getAuthenticate().getParticipantId()); @@ -89,12 +119,29 @@ public class SZRHTTPHeaderHandler implements SOAPHandler<SOAPMessageContext> { else roleString = roleString + PVP_HEADER_ROLE_VALUE_DELIMITER + roleElement; } - } - setHeader(headers, "X-AUTHORIZE-ROLES", roleString); + } + + if (roleStrategy != null) { + log.trace("Generate dynamic role ... "); + try { + String dynRoleString = roleStrategy.generateRoleString(roleString); + setHeader(headers, "X-PVP-ROLES", dynRoleString); + setHeader(headers, "X-AUTHORIZE-ROLES", dynRoleString); + + } catch (EgovUtilException e) { + throw new RuntimeException("Can NOT generate dynamic VDDS role by using: " + roleStrategy.getClass().getName(), e); + + } + + } + + + + //set optional headers - String costCenterId = null; - String invoiceRecptId = null; + String costCenterId = StringUtils.EMPTY; + String invoiceRecptId = StringUtils.EMPTY; Accounting accounting = pvpTokenType.getAccounting(); if (accounting != null) { @@ -107,7 +154,7 @@ public class SZRHTTPHeaderHandler implements SOAPHandler<SOAPMessageContext> { value = value + el.getValue(); - if (costCenterId == null) + if (costCenterId.isEmpty()) costCenterId = value; else costCenterId = PVP_HEADER_VALUE_DELIMITER + value; @@ -116,6 +163,9 @@ public class SZRHTTPHeaderHandler implements SOAPHandler<SOAPMessageContext> { } } + setHeader(headers, "X-PVP-COST-CENTER-ID", costCenterId); + setHeader(headers, "X-PVP-INVOICE-RECPT-ID", invoiceRecptId); + setHeader(headers, "X-ACCOUNTING-CostCenterId", costCenterId); setHeader(headers, "X-ACCOUNTING-InvoiceRecptId", invoiceRecptId); @@ -129,7 +179,7 @@ public class SZRHTTPHeaderHandler implements SOAPHandler<SOAPMessageContext> { } private void setHeader(Map<String, List> headers, String name, String value) { if (MiscUtil.isEmpty(value)) - headers.put(name, null); + headers.put(name, Collections.singletonList(StringUtils.EMPTY)); else headers.put(name, Collections.singletonList(value)); diff --git a/src/main/java/at/gv/util/client/szr/SZRClient.java b/src/main/java/at/gv/util/client/szr/SZRClient.java index ad54d2f..67813bd 100644 --- a/src/main/java/at/gv/util/client/szr/SZRClient.java +++ b/src/main/java/at/gv/util/client/szr/SZRClient.java @@ -104,7 +104,7 @@ public class SZRClient { } - public String transformBPK(PersonInfoType personInfo, String inputBpk, String inputTarget, String target, String vkz) throws SZRException, EgovUtilException { + public FremdBPKType transformBPK(PersonInfoType personInfo, String inputBpk, String inputTarget, String target, String vkz) throws SZRException, EgovUtilException { MiscUtil.assertNotNull(personInfo, "personInfo"); MiscUtil.assertNotNull(target, "target"); String targetPrefix = "urn:publicid:gv.at:cdid+"; @@ -135,7 +135,7 @@ public class SZRClient { if (response == null) { return null; } - return response.get(0).getFremdBPK(); + return response.get(0); } @SuppressWarnings({ "rawtypes", "unchecked" }) |