All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.dmfs.oauth2.client.utils.StructuredStringFragment Maven / Gradle / Ivy

There is a newer version: 0.22.1
Show newest version
/*
 * Copyright (C) 2016 Marten Gajda 
 *
 *
 * 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 org.dmfs.oauth2.client.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Iterator;

import org.dmfs.httpessentials.parameters.Parameter;
import org.dmfs.httpessentials.parameters.ParameterType;
import org.dmfs.httpessentials.parameters.Parametrized;
import org.dmfs.iterables.CsvIterable;
import org.dmfs.iterators.AbstractConvertedIterator.Converter;
import org.dmfs.iterators.AbstractFilteredIterator.IteratorFilter;
import org.dmfs.iterators.ConvertedIterator;
import org.dmfs.iterators.FilteredIterator;


/**
 * Provides access to individual fields of x-www-form-url-encoded fragments.
 * 
 * @author Marten Gajda 
 */
public final class StructuredStringFragment implements Parametrized
{

	private final String mFragment;
	private final Iterable mParts;


	public StructuredStringFragment(String fragment)
	{
		mFragment = fragment;
		mParts = new CsvIterable(fragment, '&');
	}


	@Override
	public  Parameter firstParameter(final ParameterType parameterType, final T defaultValue)
	{
		Iterator> iterator = parameters(parameterType);
		if (iterator.hasNext())
		{
			return iterator.next();
		}
		return parameterType.entity(defaultValue);
	}


	@Override
	public  Iterator> parameters(final ParameterType parameterType)
	{
		return new ConvertedIterator, String>(new FilteredIterator(mParts.iterator(), new IteratorFilter()
		{
			public boolean iterate(String element)
			{
				String paramName = parameterType.name();
				int paramNameLen = paramName.length();
				return element.startsWith(paramName)
					&& (element.length() == paramNameLen || element.length() > paramNameLen && element.charAt(paramNameLen) == '=');
			}
		}), new Converter, String>()
		{

			@Override
			public Parameter convert(String element)
			{
				String value = element.substring(parameterType.name().length());
				if (value.length() > 0)
				{
					try
					{
						return parameterType.entityFromString(URLDecoder.decode(value.substring(1), "UTF-8"));
					}
					catch (UnsupportedEncodingException e)
					{
						throw new RuntimeException("UTF-8 encoding not supported!", e);
					}
				}
				return parameterType.entityFromString("");
			}
		});
	}


	@Override
	public  boolean hasParameter(final ParameterType parameterType)
	{
		return parameters(parameterType).hasNext();
	}


	@Override
	public String toString()
	{
		return mFragment;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy