001package org.apache.archiva.redback.integration;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.commons.lang3.StringUtils;
023
024import java.util.Properties;
025
026/**
027 * Collection of Utility methods useful in an Http environment.
028 *
029 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
030 *
031 */
032public class HttpUtils
033{
034    /**
035     * Convert typical complex header into properties.
036     *
037     *
038     * Example:
039     *
040     * <code>
041     * realm="Somewhere Over The Rainbow", domain="kansas.co.us", nonce="65743ABCF"
042     * </code>
043     *
044     * <p>becomes</p>
045     *
046     * <code>
047     * Map ( "realm",  "Somewhere Over The Rainbox" )
048     * Map ( "domain", "kansas.co.us" )
049     * Map ( "nonce",  "65743ABCF" )
050     * </code>
051     *
052     * @param rawheader
053     * @param majorDelim
054     * @param subDelim
055     * @return
056     */
057    public static Properties complexHeaderToProperties( String rawheader, String majorDelim, String subDelim )
058    {
059        Properties ret = new Properties();
060
061        if ( StringUtils.isEmpty( rawheader ) )
062        {
063            return ret;
064        }
065
066        String array[] = StringUtils.split( rawheader, majorDelim );
067        for ( int i = 0; i < array.length; i++ )
068        {
069            // String quotes.
070            String rawelem = StringUtils.replace( array[i], "\"", "" );
071            String parts[] = StringUtils.split( rawelem, subDelim, 2 );
072
073            ret.setProperty( StringUtils.trim( parts[0] ), StringUtils.trim( parts[1] ) );
074        }
075
076        return ret;
077    }
078}