diff options
Diffstat (limited to 'src/main/java/at/gv/util/client/mis/usp_v2')
3 files changed, 205 insertions, 63 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); + + } + } + +} |