Remove Code from cadi, it is now in authz
[aaf/cadi.git] / core / src / main / java / org / onap / aaf / cadi / wsse / XReader.java
diff --git a/core/src/main/java/org/onap/aaf/cadi/wsse/XReader.java b/core/src/main/java/org/onap/aaf/cadi/wsse/XReader.java
deleted file mode 100644 (file)
index 8fa8113..0000000
+++ /dev/null
@@ -1,416 +0,0 @@
-/*******************************************************************************\r
- * ============LICENSE_START====================================================\r
- * * org.onap.aaf\r
- * * ===========================================================================\r
- * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
- * * ===========================================================================\r
- * * Licensed under the Apache License, Version 2.0 (the "License");\r
- * * you may not use this file except in compliance with the License.\r
- * * You may obtain a copy of the License at\r
- * * \r
- *  *      http://www.apache.org/licenses/LICENSE-2.0\r
- * * \r
- *  * Unless required by applicable law or agreed to in writing, software\r
- * * distributed under the License is distributed on an "AS IS" BASIS,\r
- * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * * See the License for the specific language governing permissions and\r
- * * limitations under the License.\r
- * * ============LICENSE_END====================================================\r
- * *\r
- * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
- * *\r
- ******************************************************************************/\r
-package org.onap.aaf.cadi.wsse;\r
-\r
-import java.io.ByteArrayOutputStream;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Stack;\r
-\r
-import javax.xml.stream.XMLStreamException;\r
-\r
-/**\r
- * XReader\r
- * This class works similarly as StAX, except StAX has more behavior than is needed.  That would be ok, but \r
- * StAX also was Buffering in their code in such as way as to read most if not all the incoming stream into memory,\r
- * defeating the purpose of pre-reading only the Header\r
- * \r
- * This Reader does no back-tracking, but is able to create events based on syntax and given state only, leaving the\r
- * Read-ahead mode of the InputStream up to the other classes.\r
- * \r
- * At this time, we only implement the important events, though if this is good enough, it could be expanded, perhaps to \r
- * replace the original XMLReader from StAX.\r
- * \r
- *\r
- */\r
-// @SuppressWarnings("restriction")\r
-public class XReader {\r
-       private XEvent curr,another;\r
-       private InputStream is;\r
-       private ByteArrayOutputStream baos;\r
-       private int state, count, last;\r
-       \r
-       private Stack<Map<String,String>> nsses;\r
-       \r
-       public XReader(InputStream is) {\r
-               this.is = is;\r
-               curr = another = null;\r
-               baos = new ByteArrayOutputStream();\r
-               state = BEGIN_DOC; \r
-               count = 0;\r
-               nsses = new Stack<Map<String,String>>();\r
-       }\r
-       \r
-       public boolean hasNext() throws XMLStreamException {\r
-               if(curr==null) {\r
-                       curr = parse();\r
-               }\r
-               return curr!=null;\r
-       }\r
-\r
-       public XEvent nextEvent() {\r
-               XEvent xe = curr;\r
-               curr = null;\r
-               return xe;\r
-       }\r
-\r
-       // \r
-       // State Flags\r
-       //\r
-       // Note: The State of parsing XML can be complicated.  There are too many to cleanly keep in "booleans".  Additionally,\r
-       // there are certain checks that can be better made with Bitwise operations within switches\r
-       // Keeping track of state this way also helps us to accomplish logic without storing any back characters except one\r
-       private final static int BEGIN_DOC=  0x000001;\r
-       private final static int DOC_TYPE=   0x000002;\r
-       private final static int QUESTION_F= 0x000004;\r
-       private final static int QUESTION =  0x000008;\r
-       private final static int START_TAG = 0x000010;\r
-       private final static int END_TAG =       0x000020;\r
-       private final static int VALUE=          0x000040;\r
-       private final static int COMMENT =   0x001000;\r
-       private final static int COMMENT_E = 0x002000;\r
-       private final static int COMMENT_D1 =0x010000;\r
-       private final static int COMMENT_D2 =0x020000;\r
-       private final static int COMMENT_D3 =0x040000;\r
-       private final static int COMMENT_D4 =0x080000;\r
-       // useful combined Comment states\r
-       private final static int IN_COMMENT=COMMENT|COMMENT_E|COMMENT_D1|COMMENT_D2;\r
-       private final static int COMPLETE_COMMENT = COMMENT|COMMENT_E|COMMENT_D1|COMMENT_D2|COMMENT_D3|COMMENT_D4;\r
-       \r
-       \r
-       private XEvent parse() throws XMLStreamException {\r
-               Map<String,String> nss = nsses.isEmpty()?null:nsses.peek();\r
-\r
-               XEvent rv;\r
-               if((rv=another)!=null) { // "another" is a tag that may have needed to be created, but not \r
-                                                                // immediately returned.  Save for next parse.  If necessary, this could be turned into\r
-                                                                // a FIFO storage, but a single reference is enough for now.\r
-                       another = null;      // "rv" is now set for the Event, and will be returned.  Set to Null.\r
-               } else {\r
-                       boolean go = true;\r
-                       int c=0;\r
-                       \r
-                       try {\r
-                               while(go && (c=is.read())>=0) {\r
-                                       ++count;\r
-                                       switch(c) {\r
-                                               case '<': // Tag is opening\r
-                                                       state|=~BEGIN_DOC; // remove BEGIN_DOC flag, this is possibly an XML Doc\r
-                                                       XEvent cxe = null;\r
-                                                       if(baos.size()>0) { // If there are any characters between tags, we send as Character Event\r
-                                                               String chars = baos.toString().trim();  // Trim out WhiteSpace before and after\r
-                                                               if(chars.length()>0) { // don't send if Characters were only whitespace\r
-                                                                       cxe = new XEvent.Characters(chars);\r
-                                                                       baos.reset();\r
-                                                                       go = false;\r
-                                                               }\r
-                                                       }\r
-                                                       last = c;  // make sure "last" character is set for use in "ParseTag"\r
-                                                       Tag t = parseTag(); // call subroutine to process the tag as a unit\r
-                                                       String ns;\r
-                                                       switch(t.state&(START_TAG|END_TAG)) {\r
-                                                               case START_TAG:\r
-                                                                               nss = getNss(nss,t);                    // Only Start Tags might have NS Attributes   \r
-                                                                                                                                               // Get any NameSpace elements from tag.  If there are, nss will become \r
-                                                                                                                                               // a new Map with all the previous NSs plus the new.  This provides \r
-                                                                                                                                               // scoping behavior when used with the Stack\r
-                                                                       // drop through on purpose\r
-                                                               case END_TAG:\r
-                                                                       ns = t.prefix==null?"":nss.get(t.prefix); // Get the namespace from prefix (if exists)\r
-                                                                       break;\r
-                                                               default:\r
-                                                                       ns = "";\r
-                                                       }\r
-                                                       if(ns==null)\r
-                                                               throw new XMLStreamException("Invalid Namespace Prefix at " + count);\r
-                                                       go = false;\r
-                                                       switch(t.state) { // based on \r
-                                                         case DOC_TYPE: \r
-                                                                 rv = new XEvent.StartDocument();\r
-                                                                 break;\r
-                                                         case COMMENT:\r
-                                                                 rv = new XEvent.Comment(t.value);\r
-                                                                 break;\r
-                                                         case START_TAG:\r
-                                                                 rv = new XEvent.StartElement(ns,t.name);\r
-                                                                 nsses.push(nss);                              // Change potential scope for Namespace\r
-                                                                 break;\r
-                                                         case END_TAG:\r
-                                                                 rv = new XEvent.EndElement(ns,t.name);\r
-                                                                 nss = nsses.pop();                    // End potential scope for Namespace\r
-                                                                 break;\r
-                                                         case START_TAG|END_TAG:                       // This tag is both start/end  aka <myTag/>\r
-                                                                 rv = new XEvent.StartElement(ns,t.name);\r
-                                                                 if(last=='/')another = new XEvent.EndElement(ns,t.name);\r
-                                                       }\r
-                                                       if(cxe!=null) {     // if there is a Character Event, it actually should go first.  ow.\r
-                                                               another = rv;   // Make current Event the "another" or next event, and \r
-                                                               rv = cxe;               // send Character Event now\r
-                                                       }\r
-                                                       break;\r
-                                               case ' ':\r
-                                               case '\t':\r
-                                               case '\n':\r
-                                                       if((state&BEGIN_DOC)==BEGIN_DOC) { // if Whitespace before doc, just ignore \r
-                                                               break;\r
-                                                       }\r
-                                                       // fallthrough on purpose\r
-                                               default:\r
-                                                       if((state&BEGIN_DOC)==BEGIN_DOC) { // if there is any data at the start other than XML Tag, it's not XML\r
-                                                               throw new XMLStreamException("Parse Error: This is not an XML Doc");\r
-                                                       }\r
-                                                       baos.write(c); // save off Characters\r
-                                       }\r
-                                       last = c; // Some processing needs to know what the last character was, aka Escaped characters... ex \"\r
-                               }\r
-                       } catch (IOException e) {\r
-                               throw new XMLStreamException(e); // all errors parsing will be treated as XMLStreamErrors (like StAX)\r
-                       }\r
-                       if(c==-1 && (state&BEGIN_DOC)==BEGIN_DOC) {                        // Normally, end of stream is ok, however, we need to know if the \r
-                               throw new XMLStreamException("Premature End of File"); // document isn't an XML document, so we throw exception if it \r
-                       }                                                                                                                  // hasn't yet been determined to be an XML Doc\r
-               }\r
-               return rv;\r
-       }\r
-       \r
-       /**\r
-        * parseTag\r
-        * \r
-        * Parsing a Tag is somewhat complicated, so it's helpful to separate this process from the \r
-        * higher level Parsing effort\r
-        * @return\r
-        * @throws IOException\r
-        * @throws XMLStreamException\r
-        */\r
-       private Tag parseTag() throws IOException, XMLStreamException {\r
-               Tag tag = null;\r
-               boolean go = true;\r
-               state = 0;\r
-               int c, quote=0; // If "quote" is 0, then we're not in a quote.  We set ' (in pretag) or " in attribs accordingly to denote quoted\r
-               String prefix=null,name=null,value=null;\r
-               baos.reset();\r
-               \r
-               while(go && (c=is.read())>=0) {\r
-                       ++count;\r
-                       if(quote!=0) { // If we're in a quote, we only end if we hit another quote of the same time, not preceded by \\r
-                               if(c==quote && last!='\\') {\r
-                                       quote=0;\r
-                               } else {\r
-                                       baos.write(c);\r
-                               }\r
-                       } else if((state&COMMENT)==COMMENT) { // similar to Quote is being in a comment\r
-                               switch(c) {\r
-                                       case '-':\r
-                                               switch(state) { // XML has a complicated Quote set... <!-- --> ... we keep track if each has been met with flags. \r
-                                                       case COMMENT|COMMENT_E:\r
-                                                               state|=COMMENT_D1;\r
-                                                               break;\r
-                                                       case COMMENT|COMMENT_E|COMMENT_D1:\r
-                                                               state|=COMMENT_D2;\r
-                                                               baos.reset();                           // clear out "!--", it's a Comment\r
-                                                               break;\r
-                                                       case COMMENT|COMMENT_E|COMMENT_D1|COMMENT_D2:\r
-                                                               state|=COMMENT_D3;\r
-                                                               baos.write(c);\r
-                                                               break;\r
-                                                       case COMMENT|COMMENT_E|COMMENT_D1|COMMENT_D2|COMMENT_D3:\r
-                                                               state|=COMMENT_D4;\r
-                                                               baos.write(c);\r
-                                                               break;\r
-                                               }\r
-                                               break;\r
-                                       case '>': // Tag indicator has been found, do we have all the comment characters in line?\r
-                                               if((state&COMPLETE_COMMENT)==COMPLETE_COMMENT) {\r
-                                                       byte ba[] = baos.toByteArray();\r
-                                                       tag = new Tag(null,null, new String(ba,0,ba.length-2));\r
-                                                       baos.reset();\r
-                                                       go = false;\r
-                                                       break;\r
-                                               }\r
-                                               // fall through on purpose\r
-                                       default:\r
-                                               state&=~(COMMENT_D3|COMMENT_D4);\r
-                                               if((state&IN_COMMENT)!=IN_COMMENT) state&=~IN_COMMENT; // false alarm, it's not actually a comment\r
-                                               baos.write(c);\r
-                               }\r
-                       } else { // Normal Tag Processing loop\r
-                               switch(c) {\r
-                                       case '?': \r
-                                               switch(state & (QUESTION_F|QUESTION)) {  // Validate the state of Doc tag... <?xml ... ?>\r
-                                                       case QUESTION_F:\r
-                                                               state |= DOC_TYPE;\r
-                                                               state &= ~QUESTION_F;\r
-                                                               break;\r
-                                                       case 0:\r
-                                                               state |=QUESTION_F;\r
-                                                               break;\r
-                                                       default:\r
-                                                               throw new IOException("Bad character [?] at " + count);\r
-                                               }\r
-                                               break;\r
-                                       case '!':\r
-                                               if(last=='<') { \r
-                                                       state|=COMMENT|COMMENT_E; // likely a comment, continue processing in Comment Loop\r
-                                               }\r
-                                               baos.write(c);\r
-                                               break;\r
-                                       case '/':\r
-                                               state|=(last=='<'?END_TAG:(END_TAG|START_TAG));  // end tag indicator </xxx>, ,or both <xxx/>\r
-                                               break;\r
-                                       case ':':\r
-                                               prefix=baos.toString(); // prefix indicator\r
-                                               baos.reset();\r
-                                               break;\r
-                                       case '=':                                       // used in Attributes\r
-                                               name=baos.toString();\r
-                                               baos.reset();\r
-                                               state|=VALUE;\r
-                                               break;\r
-                                       case '>': // end the tag, which causes end of this subprocess as well as formulation of the found data\r
-                                               go = false;\r
-                                               // passthrough on purpose\r
-                                       case ' ':\r
-                                       case '\t':\r
-                                       case '\n': // white space indicates change in internal tag state, ex between name and between attributes\r
-                                               if((state&VALUE)==VALUE) {\r
-                                                       value = baos.toString();        // we're in VALUE state, add characters to Value\r
-                                               } else if(name==null) {\r
-                                                       name = baos.toString();         // we're in Name state (default) add characters to Name\r
-                                               }\r
-                                               baos.reset();                                   // we've assigned chars, reset buffer\r
-                                               if(name!=null) {                                // Name is not null, there's a tag in the offing here...\r
-                                                       Tag t = new Tag(prefix,name,value);\r
-                                                       if(tag==null) {                         // Set as the tag to return, if not exists\r
-                                                               tag = t;\r
-                                                       } else {                                        // if we already have a Tag, then we'll treat this one as an attribute\r
-                                                               tag.add(t);\r
-                                                       }\r
-                                               }\r
-                                               prefix=name=value=null;                 // reset these values in case we loop for attributes.\r
-                                               break;\r
-                                       case '\'':                                                      // is the character one of two kinds of quote?\r
-                                       case '"':\r
-                                               if(last!='\\') {\r
-                                                       quote=c;\r
-                                                       break;\r
-                                               }\r
-                                               // Fallthrough ok\r
-                                       default:\r
-                                               baos.write(c);                                  // write any unprocessed bytes into buffer\r
-                                               \r
-                               }\r
-                       }\r
-                       last = c;\r
-               }\r
-               int type = state&(DOC_TYPE|COMMENT|END_TAG|START_TAG); // get just the Tag states and turn into Type for Tag\r
-               if(type==0) {\r
-                       type=START_TAG;\r
-               }\r
-               tag.state|=type;        // add the appropriate Tag States\r
-               return tag;\r
-       }\r
-\r
-       /**\r
-        * getNSS\r
-        * \r
-        * If the tag contains some Namespace attributes, create a new nss from the passed in one, copy all into it, then add\r
-        * This provides Scoping behavior\r
-        * \r
-        * if Nss is null in the first place, create an new nss, so we don't have to deal with null Maps.\r
-        * \r
-        * @param nss\r
-        * @param t\r
-        * @return\r
-        */\r
-       private Map<String, String> getNss(Map<String, String> nss, Tag t) {\r
-               Map<String,String> newnss = null;\r
-               if(t.attribs!=null) {\r
-                       for(Tag tag : t.attribs) {\r
-                               if("xmlns".equals(tag.prefix)) {\r
-                                       if(newnss==null) {\r
-                                               newnss = new HashMap<String,String>();\r
-                                               if(nss!=null)newnss.putAll(nss);\r
-                                       }\r
-                                       newnss.put(tag.name, tag.value);\r
-                               }\r
-                       }\r
-               }\r
-               return newnss==null?(nss==null?new HashMap<String,String>():nss):newnss;\r
-       }\r
-\r
-       /**\r
-        * The result of the parseTag method\r
-        * \r
-        * Data is split up into prefix, name and value portions. "Tags" with Values that are inside a Tag are known in XLM\r
-        * as Attributes.  \r
-        * \r
-        *\r
-        */\r
-       public class Tag {\r
-               public int state;\r
-               public String prefix,name,value;\r
-               public List<Tag> attribs;\r
-\r
-               public Tag(String prefix, String name, String value) {\r
-                       this.prefix = prefix;\r
-                       this.name = name;\r
-                       this.value = value;\r
-                       attribs = null;  \r
-               }\r
-\r
-               /**\r
-                * add an attribute\r
-                * Not all tags need attributes... lazy instantiate to save time and memory\r
-                * @param tag\r
-                */\r
-               public void add(Tag attrib) {\r
-                       if(attribs == null) {\r
-                               attribs = new ArrayList<Tag>();\r
-                       }\r
-                       attribs.add(attrib);\r
-               }\r
-               \r
-               public String toString() {\r
-                       StringBuffer sb = new StringBuffer();\r
-                       if(prefix!=null) {\r
-                               sb.append(prefix);\r
-                               sb.append(':');\r
-                       }\r
-                       sb.append(name==null?"!!ERROR!!":name);\r
-\r
-                       char quote = ((state&DOC_TYPE)==DOC_TYPE)?'\'':'"';\r
-                       if(value!=null) {\r
-                               sb.append('=');\r
-                               sb.append(quote);\r
-                               sb.append(value);\r
-                               sb.append(quote);\r
-                       }\r
-                       return sb.toString();\r
-               }\r
-       }\r
-\r
-}\r