<commit_before>/**
 * 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 de.zib.gndms.gndmc.security.SetupSSL;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @date: 14.03.12
 * @time: 13:06
 * @author: <NAME>
 * @email: <EMAIL>
 */
public class HTTPGetter {
    
    private String keyStoreLocation;
    private String trustStoreLocation;
    private String password;

    final private Map< Integer, EnhancedResponseExtractor > extractorMap;
    
    public HTTPGetter() {
        extractorMap = new HashMap< Integer, EnhancedResponseExtractor >();
        resetExtractorMap();
    }
    
    public void setupSSL( ) throws
            IOException,
            NoSuchAlgorithmException,
            KeyStoreException,
            CertificateException,
            UnrecoverableKeyException,
            KeyManagementException {
        SetupSSL setupSSL = new SetupSSL();
        setupSSL.setKeystoreLocation( keyStoreLocation );
        setupSSL.setTrustStoreLocation(trustStoreLocation);

        setupSSL.prepareUserCert(password.toCharArray(), password.toCharArray());
        setupSSL.setupDefaultSSLContext();
    }

    public void resetExtractorMap() {
        extractorMap.clear();
        extractorMap.put(0, new RedirectResponseExtractor());
    }
    
    public void setExtractor( int httpStatusCode, EnhancedResponseExtractor responseExtractor ) {
        extractorMap.put( httpStatusCode, responseExtractor );
    }
    
    public int get( String url ) {
        EnhancedResponseExtractor responseExtractor = get( url, null );
        int statusCode = responseExtractor.getStatusCode();

        // redirect as long as needed
        while( 300 <= statusCode && statusCode < 400 ) {
            final List< String > cookies = DefaultResponseExtractor.getCookies( responseExtractor.getHeaders() );
            final String location = DefaultResponseExtractor.getLocation( responseExtractor.getHeaders() );

            responseExtractor = get( location, new RequestCallback() {
                @Override
                public void doWithRequest( ClientHttpRequest request ) throws IOException {
                    for( String c: cookies )
                        request.getHeaders().add( "Cookie", c.split( ";", 2 )[0] );
                }
            });

            statusCode = responseExtractor.getStatusCode();
        }

        return statusCode;
    }

    EnhancedResponseExtractor get( String url, RequestCallback requestCallback ) {
        RestTemplate rt = new RestTemplate();
        // use the list as indirect pointer
        final List< EnhancedResponseExtractor > enhancedResponseExtractor = new LinkedList<EnhancedResponseExtractor>();

        rt.execute(url, HttpMethod.GET, requestCallback, new ResponseExtractor<Object>() {

            @Override
            public Object extractData( ClientHttpResponse response ) throws IOException {
                int statusCode = response.getStatusCode().value();
                
                EnhancedResponseExtractor ere = extractorMap.get( statusCode );
                if( null == ere )
                    ere = extractorMap.get( 0 );
                
                enhancedResponseExtractor.add( ere );
                return ere.extractData( response );
            }

        } );

        return enhancedResponseExtractor.get( 0 );
    }
    
    public String getKeyStoreLocation() {
        return keyStoreLocation;
    }

    public void setKeyStoreLocation(String keyStoreLocation) {
        this.keyStoreLocation = keyStoreLocation;
    }

    public String getTrustStoreLocation() {
        return trustStoreLocation;
    }

    public void setTrustStoreLocation(String trustStoreLocation) {
        this.trustStoreLocation = trustStoreLocation;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}<commit_msg>Codestyle<commit_after>/**
 * 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 de.zib.gndms.gndmc.security.SetupSSL;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.RequestCallback;
import org.springframework.web.client.ResponseExtractor;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @date: 14.03.12
 * @time: 13:06
 * @author: <NAME>
 * @email: <EMAIL>
 */
public class HTTPGetter {
    
    private String keyStoreLocation;
    private String trustStoreLocation;
    private String password; // TODO: don't use same password for keystore and key?!

    final private Map< Integer, EnhancedResponseExtractor > extractorMap;
    
    public HTTPGetter() {
        extractorMap = new HashMap< Integer, EnhancedResponseExtractor >();
        resetExtractorMap();
    }

    public void resetExtractorMap() {
        extractorMap.clear();
        extractorMap.put( 0, new RedirectResponseExtractor() );
    }
    
    public EnhancedResponseExtractor getResponseExtractor( int statusCode ) {
        return extractorMap.get( statusCode );
    }

    public void setupSSL( ) throws
            IOException,
            NoSuchAlgorithmException,
            KeyStoreException,
            CertificateException,
            UnrecoverableKeyException,
            KeyManagementException {
        SetupSSL setupSSL = new SetupSSL();
        setupSSL.setKeystoreLocation( keyStoreLocation );
        setupSSL.setTrustStoreLocation( trustStoreLocation );

        setupSSL.prepareUserCert( password.toCharArray(), password.toCharArray() );
        setupSSL.setupDefaultSSLContext();
    }

    public void setExtractor( int httpStatusCode, EnhancedResponseExtractor responseExtractor ) {
        extractorMap.put( httpStatusCode, responseExtractor );
    }
    
    public int get( String url ) {
        EnhancedResponseExtractor responseExtractor = get( url, null );
        int statusCode = responseExtractor.getStatusCode();

        // redirect as long as needed
        while( 300 <= statusCode && statusCode < 400 ) {
            final List< String > cookies = DefaultResponseExtractor.getCookies( responseExtractor.getHeaders() );
            final String location = DefaultResponseExtractor.getLocation( responseExtractor.getHeaders() );

            responseExtractor = get( location, new RequestCallback() {
                @Override
                public void doWithRequest( ClientHttpRequest request ) throws IOException {
                    for( String c: cookies )
                        request.getHeaders().add( "Cookie", c.split( ";", 2 )[0] );
                }
            });

            statusCode = responseExtractor.getStatusCode();
        }

        return statusCode;
    }

    EnhancedResponseExtractor get( String url, RequestCallback requestCallback ) {
        RestTemplate rt = new RestTemplate();
        // use the list as indirect pointer
        final List< EnhancedResponseExtractor > enhancedResponseExtractor = new LinkedList<EnhancedResponseExtractor>();

        rt.execute( url, HttpMethod.GET, requestCallback, new ResponseExtractor< Object >() {

            // call the EnhancedResponseExtractor registered for this response.statusCode
            @Override
            public Object extractData( ClientHttpResponse response ) throws IOException {
                int statusCode = response.getStatusCode().value();
                
                EnhancedResponseExtractor ere = extractorMap.get( statusCode );
                if( null == ere )
                    ere = extractorMap.get( 0 );
                
                enhancedResponseExtractor.add( ere );
                return ere.extractData( response );
            }

        } );

        return enhancedResponseExtractor.get( 0 );
    }
    
    public String getKeyStoreLocation() {
        return keyStoreLocation;
    }

    public void setKeyStoreLocation( String keyStoreLocation ) {
        this.keyStoreLocation = keyStoreLocation;
    }

    public String getTrustStoreLocation() {
        return trustStoreLocation;
    }

    public void setTrustStoreLocation( String trustStoreLocation ) {
        this.trustStoreLocation = trustStoreLocation;
    }

    public void setPassword( String password ) {
        this.password = password;
    }
}
{'fraction_non_alphanumeric': 0.08075126322814377, 'fraction_numerical': 0.006292306225569645, 'mean_word_length': 2.8326635001826816, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1885552', 'n_tokens_mistral': 2682, 'n_tokens_neox': 2574, 'n_words': 704}