
com.englishtown.vertx.jersey.impl.DefaultJerseyOptions Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of vertx-jersey Show documentation
Show all versions of vertx-jersey Show documentation
Allows creating JAX-RS jersey resources that will handle incoming http requests to vert.x
The newest version!
/*
* The MIT License (MIT)
* Copyright © 2013 Englishtown
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.englishtown.vertx.jersey.impl;
import com.englishtown.vertx.jersey.JerseyOptions;
import com.englishtown.vertx.jersey.JerseyServerOptions;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.core.net.JksOptions;
import javax.inject.Inject;
import java.net.URI;
import java.util.*;
import java.util.function.Consumer;
/**
* Default {@link com.englishtown.vertx.jersey.JerseyOptions} implementation
*/
public class DefaultJerseyOptions implements JerseyOptions, JerseyServerOptions {
public final static String CONFIG_HOST = "host";
public final static String CONFIG_PORT = "port";
public final static String CONFIG_SSL = "ssl";
public final static String CONFIG_JKS_OPTIONS = "jks_options";
public final static String CONFIG_RECEIVE_BUFFER_SIZE = "receive_buffer_size";
public final static String CONFIG_BACKLOG_SIZE = "backlog_size";
public final static String CONFIG_RESOURCE_CONFIG = "resource_config";
public final static String CONFIG_PROPERTIES = "properties";
public final static String CONFIG_COMPRESSION_SUPPORTED = "compression_supported";
public static final String CONFIG_BASE_PATH = "base_path";
public static final String CONFIG_MAX_BODY_SIZE = "max_body_size";
public static final String CONFIG_RESOURCES = "resources";
public static final String CONFIG_PACKAGES = "packages";
public static final String CONFIG_FEATURES = "features";
public static final String CONFIG_COMPONENTS = "components";
public static final String CONFIG_BINDERS = "binders";
public static final String CONFIG_INSTANCES = "instances";
public static final int DEFAULT_MAX_BODY_SIZE = 1024 * 1000; // Default max body size to 1MB
private JsonObject config;
private HttpServerOptions serverOptions;
@Inject
public DefaultJerseyOptions(Vertx vertx) {
this(getConfig(vertx));
}
public DefaultJerseyOptions(JsonObject config) {
this.config = config;
}
private static JsonObject getConfig(Vertx vertx) {
JsonObject config = vertx.getOrCreateContext().config();
return config.getJsonObject("jersey", config);
}
/**
* Returns a list of packages to be scanned for resources and components
*
* @return
*/
@Override
public List getPackages() {
List list = new ArrayList<>();
Consumer reader = array -> {
if ((array != null && !array.isEmpty())) {
for (int i = 0; i < array.size(); i++) {
list.add(array.getString(i));
}
}
};
JsonArray resources = config.getJsonArray(CONFIG_RESOURCES, null);
JsonArray packages = config.getJsonArray(CONFIG_PACKAGES, null);
reader.accept(resources);
reader.accept(packages);
return list;
}
/**
* Optional additional properties to be applied to Jersey resource configuration
*
* @return
*/
@Override
public Map getProperties() {
JsonObject json = null;
JsonObject tmp;
tmp = config.getJsonObject(CONFIG_PROPERTIES);
if (tmp != null) {
json = tmp;
}
tmp = config.getJsonObject(CONFIG_RESOURCE_CONFIG);
if (tmp != null) {
if (json == null) {
json = tmp;
} else {
json.mergeIn(tmp);
}
}
return json == null ? null : json.getMap();
}
/**
* List of components to be registered (features etc.)
*
* @return
*/
@Override
public Set> getComponents() {
Set> set = new HashSet<>();
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Consumer reader = array -> {
if (array != null && array.size() > 0) {
for (int i = 0; i < array.size(); i++) {
try {
set.add(cl.loadClass(array.getString(i)));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
}
};
JsonArray features = config.getJsonArray(CONFIG_FEATURES, null);
JsonArray components = config.getJsonArray(CONFIG_COMPONENTS, null);
reader.accept(features);
reader.accept(components);
return set;
}
/**
* Optional list of singleton instances to be registered (hk2 binders etc.)
*
* @return
*/
@Override
public Set
© 2015 - 2025 Weber Informatics LLC | Privacy Policy