/*
 * JBoss, Home of Professional Open Source
 * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @author tags. All rights reserved.
 * See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License, v. 2.1.
 * This program is distributed in the hope that it will be useful, but WITHOUT A
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License,
 * v.2.1 along with this distribution; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */

package org.jboss.as.console.client.shared.dispatch.impl;


import org.jboss.as.console.client.Console;
import org.jboss.as.console.client.shared.dispatch.Result;
import org.jboss.dmr.client.ModelNode;
import org.jboss.dmr.client.Property;

import java.util.List;

/**
 * @author Heiko Braun
 * @date 3/17/11
 */
public class DMRResponse implements Result<ModelNode> {
    private String responseText;
    private String contentType;

    public DMRResponse(String responseText, String contentType) {
        this.responseText = responseText;
        this.contentType = contentType;
    }

    @Override
    public ModelNode get() {
        ModelNode response = ModelNode.fromBase64(responseText);

        boolean hasReloadFlag = false;

        if(response.hasDefined("response-headers"))
        {
            List<Property> headers = response.get("response-headers").asPropertyList();
            for(Property header : headers) {
                if("process-state".equals(header.getName())) {
                    if("reload-required".equals(header.getValue().asString())) {
                        hasReloadFlag=true;
                    }
                }
            }


        }

        // update the state anytime
        Console.MODULES.getReloadState().setReloadRequired(hasReloadFlag);

        return response;
    }
}

@@ -1328,16 +1328,223 @@
 Node%3E %7B%0A
+%0A    private static final String RESPONSE_HEADERS = %22response-headers%22;%0A    private static final String PROCESS_STATE = %22process-state%22;%0A    private static final String RELOAD_REQUIRED = %22reload-required%22;%0A%0A
     priv
@@ -1861,24 +1861,279 @@
 onseText);%0A%0A
+        // update the state anytime%0A        Console.MODULES.getReloadState().setReloadRequired(%0A                parseReloadRequired(response)%0A        );%0A%0A        return response;%0A    %7D%0A%0A    private static boolean parseReloadRequired(ModelNode response) %7B%0A
         bool
@@ -2159,17 +2159,16 @@
  false;%0A
-%0A
         
@@ -2190,34 +2190,32 @@
 Defined(
-%22response-headers%22
+RESPONSE_HEADERS
 ))%0A     
@@ -2269,34 +2269,32 @@
 nse.get(
-%22response-headers%22
+RESPONSE_HEADERS
 ).asProp
@@ -2369,31 +2369,29 @@
      if(
-%22process-state%22
+PROCESS_STATE
 .equals(
@@ -2434,33 +2434,31 @@
      if(
-%22reload-required%22
+RELOAD_REQUIRED
 .equals(
@@ -2588,18 +2588,16 @@
       %7D%0A
-%0A%0A
         
@@ -2598,25 +2598,24 @@
       %7D%0A
-%0A
         
 // updat
@@ -2610,135 +2610,28 @@
     
-// update the state anytime%0A        Console.MODULES.getReloadState().setReloadRequired(hasReloadFlag);%0A%0A        return response
+return hasReloadFlag
 ;%0A  

optimize parsing of response headers