/**
 * Copyright 2008-2012 Zuse Institute Berlin (ZIB)
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package de.zib.gndms.gndmc.utils;

import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.LinkedList;
import java.util.List;

/**
 * @date: 14.03.12
 * @time: 13:19
 * @author: Jörg Bachmann
 * @email: bachmann@zib.de
 */
public class RedirectResponseExtractor extends DefaultResponseExtractor implements EnhancedResponseExtractor {

    private List< String > cookie = null;
    private String location = null;

    @Override
    public void extractData( final String url, final ClientHttpResponse response ) throws IOException {

        super.extractData( url, response );

        for( String header: getHeaders().keySet() ) {
            if( "Set-Cookie".equals( header ) ) {
                cookie = new LinkedList< String >();

                for( String value: getHeaders().get( header ) ) {
                    cookie.add( value );
                }
            }
            else if( "Location".equals( header ) ) {
                location = URLDecoder.decode(getHeaders().get(header).get(0));
            }
        }
    }
    
    public List< String > getCookie() {
        return cookie;
    }

    public String getLocation() {
        return location;
    }
}

@@ -1262,24 +1262,73 @@
 esponse );%0A%0A
+        cookie = null;%0A        location = null;%0A%0A
         for(
@@ -1710,16 +1710,17 @@
 .decode(
+ 
 getHeade
@@ -1732,22 +1732,35 @@
 get(
+ 
 header
+ 
 ).get(
-0)
+ 0 ), %22utf8%22 
 );%0A 
@@ -1777,24 +1777,185 @@
  %7D%0A        %7D
+%0A%0A        if( null == location ) %7B%0A            throw new IllegalArgumentException( %22Got a redirection but no Location (HTTP Header) to redirect to.%22 );%0A        %7D
 %0A    %7D%0A    %0A

Handle missing Location in HTTP Redirection