![JAR search and dependency download from the Maven repository](/logo.png)
fr.whimtrip.ext.jwhtscrapper.service.scoped.req.RequestUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of whimtrip-ext-scrapper Show documentation
Show all versions of whimtrip-ext-scrapper Show documentation
Fully featured highly pluggable and customizable Java scrapping framework
The newest version!
package fr.whimtrip.ext.jwhtscrapper.service.scoped.req;
import fr.whimtrip.ext.jwhtscrapper.annotation.Field;
import fr.whimtrip.ext.jwhtscrapper.enm.ProxyStatus;
import fr.whimtrip.ext.jwhtscrapper.intfr.Proxy;
import fr.whimtrip.ext.jwhtscrapper.intfr.ProxyFinder;
import fr.whimtrip.ext.jwhtscrapper.service.holder.PostField;
import org.asynchttpclient.BoundRequestBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* Part of project jwht-scrapper
* Created on 28/07/18
*
* Some static HTTP related utils methods.
*
* @author Louis-wht
* @since 1.0.0
*/
public class RequestUtils {
/**
* @param req the HTTP request builder to reset proxy for.
* @param proxyFinder the proxy finder implementation to find a proxy from.
* @return the {@link Proxy} found after setting it to the HTTP request builder instance.
*/
public static Proxy resetProxy(@NotNull final BoundRequestBuilder req, @NotNull final ProxyFinder proxyFinder) {
Proxy proxy = proxyFinder.findOneRandom();
req.setProxyServer(proxy.getProxyServer());
return proxy;
}
/**
* @param proxy the proxy to modify.
* @param status the new status to attribute it
* @param proxyFinder the proxyFinder to use to persist the new proxy state
* (if supported by the current implementation of {@link ProxyFinder},
* see {@link ProxyFinder#persistProxy(Proxy)}).
*/
public static void setProxyStatus(@Nullable final Proxy proxy, @NotNull final ProxyStatus status, @Nullable final ProxyFinder proxyFinder) {
if(proxy != null && proxyFinder != null) {
proxy.setStatus(status);
proxyFinder.persistProxy(proxy);
}
}
/**
*
* Will transform {@link Field} into {@link PostField}.
*
* @param fields the annotations fields to build.
* @return corresponding {@link PostField} list.
*/
public static List buildFields(Field[] fields) {
List parsedFields = new ArrayList<>();
for(Field field : fields)
{
parsedFields.add(new PostField(field.name(), field.value().toString()));
}
return parsedFields;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy