io.hyperfoil.http.html.EmbeddedResourceHandlerBuilder Maven / Gradle / Ivy
package io.hyperfoil.http.html;
import io.hyperfoil.api.config.BenchmarkDefinitionException;
import io.hyperfoil.api.processor.HttpRequestProcessorBuilder;
import io.hyperfoil.api.processor.Processor;
import io.hyperfoil.core.builders.ServiceLoadedBuilderProvider;
import io.hyperfoil.core.handlers.MultiProcessor;
/**
* Handles <img src="...">
, <link href="...">
,
* <embed src="...">
, <frame src="...">
,
* <iframe src="...">
, <object data="...">
and <script src="...">
.
*
* Does not handle <source src="...">
or <track src="...">
because browser
* would choose only one of the options.
*/
public class EmbeddedResourceHandlerBuilder implements HtmlHandler.TagHandlerBuilder {
private static final String[] TAGS = { "img", "link", "embed", "frame", "iframe", "object", "script" };
private static final String[] ATTRS = { "src", "href", "src", "src", "src", "data", "src" };
private boolean ignoreExternal = true;
private Processor.Builder> processor;
private FetchResourceHandler.Builder fetchResource;
/**
* Ignore resources hosted on servers that are not covered in the http
section.
*
* @param ignoreExternal Ignore?
* @return Self.
*/
public EmbeddedResourceHandlerBuilder ignoreExternal(boolean ignoreExternal) {
this.ignoreExternal = ignoreExternal;
return this;
}
/**
* Automatically download referenced resource.
*
* @return Builder.
*/
public FetchResourceHandler.Builder fetchResource() {
return this.fetchResource = new FetchResourceHandler.Builder();
}
public EmbeddedResourceHandlerBuilder processor(Processor.Builder> processor) {
if (this.processor == null) {
this.processor = processor;
} else if (this.processor instanceof MultiProcessor.Builder) {
MultiProcessor.Builder> multiprocessor = (MultiProcessor.Builder>) this.processor;
multiprocessor.processor(processor);
} else {
this.processor = new MultiProcessor.Builder<>().processor(this.processor).processor(processor);
}
return this;
}
/**
* Custom processor invoked pointing to attribute data - e.g. in case of <img>
tag
* the processor gets contents of the src
attribute.
*
* @return Builder.
*/
public ServiceLoadedBuilderProvider processor() {
return new ServiceLoadedBuilderProvider<>(HttpRequestProcessorBuilder.class, this::processor);
}
@Override
public HtmlHandler.BaseTagAttributeHandler build() {
if (processor == null && fetchResource == null) {
throw new BenchmarkDefinitionException("embedded resource handler must define either processor or fetchResource!");
}
Processor processor = this.processor != null ? this.processor.build(false) : null;
FetchResourceHandler fetchResource = this.fetchResource != null ? this.fetchResource.build() : null;
return new HtmlHandler.BaseTagAttributeHandler(TAGS, ATTRS, new EmbeddedResourceProcessor(ignoreExternal, processor, fetchResource));
}
}