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

org.echocat.jomon.resources.RotatingDomainResourceRequestUriGenerator Maven / Gradle / Ivy

/*****************************************************************************************
 * *** BEGIN LICENSE BLOCK *****
 *
 * Version: MPL 2.0
 *
 * echocat Jomon, Copyright (c) 2012-2013 echocat
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * *** END LICENSE BLOCK *****
 ****************************************************************************************/

package org.echocat.jomon.resources;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;

import static org.apache.commons.lang3.StringUtils.isEmpty;

/**
 * 

Usage

* *

{@link #setBaseUri(URI) baseUri}

*
    *
  • Provide an URI with no query and no fragment.
  • *
  • Add $hostNumber$ in the URI to create the resulting uri. Example https://resources$hostNumber$.mydomain.com/
  • *
  • If set to null (default) this ResourceRequestUriGenerator will simple return the value of the delegate.
  • *
* *

{@link #setHostCount(int) hostCount}

*
    *
  • Will be used to resolve the $hostNumber$ placeholder of {@link #setBaseUri(URI) baseUri}.
  • *
  • Creation will produce a number between 1 and {@link #setHostCount(int) hostCount}.
  • *
*/ public class RotatingDomainResourceRequestUriGenerator implements ResourceRequestUriGenerator { private final ResourceRequestUriGenerator _delegate; public RotatingDomainResourceRequestUriGenerator(@Nonnull ResourceRequestUriGenerator delegate) { _delegate = delegate; } private URI _baseUri; private int _hostCount = 10; public URI getBaseUri() { return _baseUri; } public void setBaseUri(URI baseUri) { if (baseUri != null && (!isEmpty(baseUri.getRawFragment()) || !isEmpty(baseUri.getRawQuery()))) { throw new IllegalArgumentException("Provided baseUri has a fragment and/or query."); } _baseUri = baseUri; } @Nonnegative public int getHostCount() { return _hostCount; } public void setHostCount(@Nonnegative int hostCount) { _hostCount = hostCount; } @Nonnull @Override public String generate(@Nonnull Resource forResource) throws IOException { final String original = _delegate.generate(forResource); final URI baseUri = _baseUri; final String result; if (baseUri != null) { final String prefix = baseUri.toString().replace("$hostNumber$", hostNumberValueFor(forResource)); result = prefix + original; } else { result = original; } return result; } @Nonnull protected String hostNumberValueFor(@Nonnull Resource resource) throws IOException { final int hashCode = Arrays.hashCode(resource.getMd5()); final int normalizedHashCode = hashCode >= 0 ? hashCode : hashCode * -1; return Integer.toString((normalizedHashCode % _hostCount) + 1); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy