001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.directory.api.dsmlv2.actions;
021
022
023import java.io.IOException;
024
025import org.apache.directory.api.dsmlv2.Dsmlv2Container;
026import org.apache.directory.api.dsmlv2.Dsmlv2StatesEnum;
027import org.apache.directory.api.dsmlv2.GrammarAction;
028import org.xmlpull.v1.XmlPullParser;
029import org.xmlpull.v1.XmlPullParserException;
030
031
032/**
033 * The action used to read the SOAP Header
034 *
035 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
036 */
037public class ReadSoapHeader extends GrammarAction
038{
039    /**
040     * Instantiates the action.
041     */
042    public ReadSoapHeader()
043    {
044        super( "Reads SOAP header" );
045    }
046
047
048    /**
049     * {@inheritDoc}
050     */
051    public void action( Dsmlv2Container container ) throws XmlPullParserException
052    {
053        try
054        {
055            XmlPullParser xpp = container.getParser();
056            StringBuilder sb = new StringBuilder();
057
058            String startTag = xpp.getText();
059            sb.append( startTag );
060
061            // string '<' and '>'
062            startTag = startTag.substring( 1, startTag.length() - 1 );
063
064            int tagType = -1;
065            String endTag = "";
066
067            // continue parsing till we get to the end tag of SOAP header
068            // and match the tag values including the namespace
069            while ( !startTag.equals( endTag ) )
070            {
071                tagType = xpp.next();
072                endTag = xpp.getText();
073                sb.append( endTag );
074
075                if ( tagType == XmlPullParser.END_TAG )
076                {
077                    // strip '<', '/' and '>'
078                    endTag = endTag.substring( 2, endTag.length() - 1 );
079                }
080            }
081
082            // change the state to header end
083            container.setState( Dsmlv2StatesEnum.SOAP_HEADER_END_TAG );
084        }
085        catch ( IOException e )
086        {
087            e.printStackTrace();
088        }
089    }
090}