/******************************************************************************* * Copyright 2014 Federal Chancellery Austria * MOA-ID has been developed in a cooperation between BRZ, the Federal * Chancellery Austria - ICT staff unit, and Graz University of Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. ******************************************************************************/ /* * Copyright 2003 Federal Chancellery Austria * MOA-ID has been developed in a cooperation between BRZ, the Federal * Chancellery Austria - ICT staff unit, and Graz University of Technology. * * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * http://www.osor.eu/eupl/ * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. */ package at.gv.egovernment.moa.id.auth.parser; import java.util.Iterator; import java.util.List; import java.util.Vector; import org.w3c.dom.Document; import org.w3c.dom.Element; import at.gv.egovernment.moa.id.auth.data.InfoboxToken; import at.gv.egovernment.moa.id.auth.data.InfoboxTokenImpl; import at.gv.egovernment.moa.id.auth.exception.ParseException; import at.gv.egovernment.moa.logging.Logger; import at.gv.egovernment.moa.util.Constants; import at.gv.egovernment.moa.util.DOMUtils; /** * Parses and unmarshales InfoboxReadResponse. * This parser is especially used for parsing additional responses (additional to that * one containing the IdentityLink retuned from the BKU as an answer of * a <PushInfobox> request. */ public class ExtendedInfoboxReadResponseParser { /** * Hide default constructor. */ private ExtendedInfoboxReadResponseParser() { } /** * Parses and unmarshales the given infoboxReadResponse to a list of * {@link at.gv.egovernment.moa.id.auth.data.InfoboxToken InfoboxToken} objects. * The method parses the given infoboxReadResponse * * @param infoboxReadResponse The infobox read response to be unmarshaled. * @param infoboxName The name of the infobox the reponse corresponds to. * * @return A list of {@link at.gv.egovernment.moa.id.auth.data.InfoboxToken InfoboxToken} * objects. Maybe empty. * * @throws ParseException If an error occurs on parsing and unmarshaling the response. */ public static List parseInfoboxReadResponse(String infoboxReadResponse, String infoboxName) throws ParseException { Element infoboxReadResponseElem = null; try { Document doc = DOMUtils.parseDocument(infoboxReadResponse, true, Constants.ALL_SCHEMA_LOCATIONS, null); infoboxReadResponseElem = doc.getDocumentElement(); } catch (Exception e) { Logger.error("InfoboxReadResponse for \"" + infoboxName + "\"-infobox could not be parsed successfully: " + e.getMessage()); throw new ParseException("parser.01", new Object[] {infoboxName + "-InfoboxReadResponse"}); } Vector infoboxTokenList = new Vector(); if (infoboxReadResponseElem != null) { // avoid using namespace URI or prefix, because it might change within the response // (e.g.: sl11-namespace, some child sl10-namespace List infoboxReadResponseChildren = DOMUtils.getChildElements(infoboxReadResponseElem); String key = null; boolean primary = true; Element infoboxReadResponseChild = (Element)infoboxReadResponseChildren.get(0); String infoboxReadResponseChildName = infoboxReadResponseChild.getLocalName(); if (infoboxReadResponseChildName.equals("AssocArrayData")) { // get the child elements from the element List assocArrayPairs = DOMUtils.getChildElements(infoboxReadResponseChild); Iterator assocArrayPairIt = assocArrayPairs.iterator(); int pairCount = 0; // step through the elemnts while (assocArrayPairIt.hasNext()) { Element assocArrayPair = (Element)assocArrayPairIt.next(); // check if the element actually a "Pair" element and not only a "key" if (assocArrayPair.getLocalName().equals("Key")) { // do not accept only a Key throw new ParseException("parser.07", new Object[] {infoboxName}); } key = assocArrayPair.getAttribute("Key"); if (pairCount > 0) { primary = false; } pairCount++; infoboxTokenList.addAll(getTokenFromXMLOrBase64Content(assocArrayPair, infoboxName, key, primary)); } } else if (infoboxReadResponseChildName.equals("BinaryFileData")) { infoboxTokenList.addAll(getTokenFromXMLOrBase64Content(infoboxReadResponseChild, infoboxName, null, true)); } } return infoboxTokenList; } /** * Unmarshales the <XMLContent> or * <Base64Content> child of the given element to a list of * infobox token. * * @param contentParent The elment including the <XMLContent> or * <Base64Content> child to unmarshal the * infobox token from. * @param infoboxName The name of the infobox. * @param key The key of an AssocArrayData-Pair. * Maybe null. * @param primary Specifies whether this token is the first (e.g. in an * AssocArrayData) token. * * @return A infobox token list. * * @throws ParseException If the contentParent has no <XMLContent> * or <Base64Content> child or the * <XMLContent> is empty. */ public static List getTokenFromXMLOrBase64Content( Element contentParent, String infoboxName, String key, boolean primary) throws ParseException { Vector tokenList = new Vector(); // get the or List content = DOMUtils.getChildElements(contentParent); if (content.size() == 1) { Element contentElem = (Element)content.get(0); if (contentElem.getLocalName().equals("XMLContent")) { List xmlContentChildren = DOMUtils.getChildElements(contentElem); if (xmlContentChildren.size() == 0) { throw new ParseException("parser.06", new Object[] {infoboxName, "Inhalt", "XMLContent"}); } int xmlCount = 0; Iterator contentIt = xmlContentChildren.iterator(); while (contentIt.hasNext()) { Element xmlToken = (Element)contentIt.next(); if (xmlCount > 0) { primary = false; } InfoboxToken infoboxToken = new InfoboxTokenImpl(key, primary, xmlToken); tokenList.add(infoboxToken); xmlCount++; } } else { String base64Token = contentElem.getFirstChild().getNodeValue(); InfoboxToken infoboxToken = new InfoboxTokenImpl(key, primary, base64Token); tokenList.add(infoboxToken); } } else { throw new ParseException("parser.06", new Object[] {infoboxName, "XMLContent oder Base64Content", contentParent.getLocalName()}); } return tokenList; } }