I am building a fat jar for a Vertx-Web application. I would like to serve some static files. I packaged the jar file, with webroot folder. See below screenshot for my jar structure:
I was able to load the webroot/static/test.html file by doing:
routingContext.response().sendFile("webroot/static/test.html");
However, I am not able to get the static handler to work. Below is my full code:
package com.jdescript;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServer;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.StaticHandler;
public class WebVerticle extends AbstractVerticle {
private HttpServer httpServer;
#Override
public void start() throws IOException {
httpServer = vertx.createHttpServer();
Router router = Router.router(vertx);
router.route("/static/*").handler(StaticHandler.create());
router.route("/test").handler(routingContext -> {
routingContext.response().sendFile("webroot/static/test.html");
});
httpServer.requestHandler(router::accept).listen(9999);
}
}
In the above example, http://localhost:9999/static/test.html will say "Not Found", while http://localhost:9999/test will render test.html.
Any help will be appreciated.
Was answered by the Vert.x group at https://groups.google.com/forum/?fromgroups#!topic/vertx/yKInZuYcqDE. My webroot should look like "webroot/test.html", instead of "webroot/static/test.html".
Related
I am trying to extend OpenTelemetry java agent and I don't see any indication it is trying to load my jar.
I am running the following cmd:
java -javaagent:../src/main/resources/opentelemetry-javaagent.jar -Dotel.javaagent.configuration-file=../src/main/resources/agent-prp.properties -jar simple-service-1.0-SNAPSHOT-jar-with-dependencies.jar
my config file is (the attributes are working):
otel.javaagent.extensions=/Users/foo/source/simple-service/src/main/resources/span-processor-1.0-SNAPSHOT.jar
otel.resource.attributes=service.name=foooBarr
my extension is:
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class FooSpanProcessor implements SpanProcessor {
private static final ObjectMapper objMapper = new ObjectMapper();
private static final Logger log = LoggerFactory.getLogger(FooSpanProcessor.class);
#Override
public void onStart(Context parentContext, ReadWriteSpan span) {
log.error("fffffffffff");
span.setAttribute("fooToken", FooProperties.INSTANCE.fooToken);
span.setAttribute("service.name", FooProperties.INSTANCE.serviceName);
span.setAttribute("runtime", FooProperties.INSTANCE.javaVersion);
span.setAttribute("tracerVersion", "0.0.1");
span.setAttribute("framework", FooProperties.INSTANCE.frameWork);
span.setAttribute("envs", FooProperties.INSTANCE.environment);
span.setAttribute("metaData", FooProperties.INSTANCE.metadata);
}
#Override
public boolean isStartRequired() {
return true;
}
#Override
public void onEnd(ReadableSpan span) {
}
....
I don't see any indication that my extension is loaded, I don't see any of my parameters on the produced spans. can any body help me?
"otel.javaagent.extensions" is not supported in the config file. add it with -D.
add the span processor to a config call implementing sdkTracerProviderConfigurer
I am trying to read information from MANIFEST.MF while initializing JAX-RS. I use Glassfish app server.
My first idea was to use the following piece of code:
Properties prop = new Properties();
prop.load(getClasses().getClass().getResourceAsStream("/META-INF/MANIFEST.MF"));
String version = prop.getProperty("Implementation-Version");
But this code reads info from another MANIFEST.MF, not from my WAR so I need to use ServletContext:
package com.remal.forum.service.configuration.rest;
import java.io.InputStream;
import java.util.jar.Manifest;
import javax.servlet.ServletContext;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Context;
#ApplicationPath("api")
public class Configurator extends Application {
#Context
protected ServletContext servletContext;
public Configurator() {
...
InputStream inputStream = servletContext.getResourceAsStream("/META-INF/MANIFEST.MF");
Manifest manifest = new Manifest(inputStream);
manifest.getAttributes("Implementation-Version");
...
}
}
But the injected servletContext is always null. What is wrong here?
I'm working on a simple automated java program for using the box api and am trying to use json. I've borrowed the first part of the checkstyle sample code from the Github's repo example SearchExamplesAsAppUser, figuring it should work.
When I run it, I get a this error
java.lang.NoClassDefFoundError: org/bouncycastle/operator/OperatorCreationException
The problem seems to be stemming from the statement:
api = BoxDeveloperEditionAPIConnection.getAppUserConnection(USER_ID, boxConfig, accessTokenCache);
The Jars which I am using are (aside from commons, all recommended by box):
bcpkix-jdk15on-1.52.jar
bcprov-jdk15on-1.52.jar
box-java-sdk-2.14.1.jar
jose4j-0.4.4.jar
minimal-json-0.9.1.jar
commons-codec-1.9.jar
commons-httpclient-3.1.jar
commons-logging-1.2.jar
I am using netbeans so all of the jars above are listed under the libraries to use fr compilation.
The code is as follows:
package boxapitest;
import com.box.sdk.BoxAPIConnection;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.box.sdk.BoxConfig;
import com.box.sdk.BoxDeveloperEditionAPIConnection;
import com.box.sdk.BoxItem;
import com.box.sdk.BoxMetadataFilter;
import com.box.sdk.BoxSearch;
import com.box.sdk.BoxSearchParameters;
import com.box.sdk.BoxUser;
import com.box.sdk.DateRange;
import com.box.sdk.IAccessTokenCache;
import com.box.sdk.InMemoryLRUAccessTokenCache;
import com.box.sdk.PartialCollection;
import com.box.sdk.SizeRange;
public final class BoxAPITest {
private static final String USER_ID = "***email address removed for privacy***";
private static final int MAX_DEPTH = 1;
private static final int MAX_CACHE_ENTRIES = 100;
private static BoxDeveloperEditionAPIConnection api;
/**
* #param args the command line arguments
* #throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
// Turn off logging to prevent polluting the output.
Logger.getLogger("com.box.sdk").setLevel(Level.SEVERE);
//It is a best practice to use an access token cache to prevent unneeded requests to Box for access tokens.
//For production applications it is recommended to use a distributed cache like Memcached or Redis, and to
//implement IAccessTokenCache to store and retrieve access tokens appropriately for your environment.
IAccessTokenCache accessTokenCache = new InMemoryLRUAccessTokenCache(MAX_CACHE_ENTRIES);
Reader reader;
reader = new FileReader("\\My Path\\file.json");
BoxConfig boxConfig = BoxConfig.readFrom(reader);
api = BoxDeveloperEditionAPIConnection.getAppUserConnection(USER_ID, boxConfig, accessTokenCache);
//api = BoxAPIConnection.getAppUserConnection(USER_ID, boxConfig, accessTokenCache);
BoxUser.Info userInfo = BoxUser.getCurrentUser(api).getInfo();
System.out.format("Welcome, %s!\n\n", userInfo.getName());
}
}
Any assistance would be most appreciated.
Bentaye actually provided the answer. One of my jars was corrupt.
I am trying to add a resource to an Arquillian shrink-wrap archive and to load it via the classpath. However, I did not manage to make it working. I tried using addAsResource() and addAsWebInfResource(). The version below is the one I think should work, but does not. Any solutions?
import static org.junit.Assert.assertNotNull;
import java.io.InputStream;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
#RunWith(Arquillian.class)
public class ResourceLoadingTest {
#Deployment
public static WebArchive createDeployment() {
WebArchive archive = ShrinkWrap.create(WebArchive.class)
.addAsWebInfResource(new StringAsset("Hello World"),
"classes/test.properties");
System.out.println(archive.toString(true));
return archive;
}
#Test
public void testLoadResource() {
InputStream stream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("/test.properties");
assertNotNull(stream);
}
}
Console output:
411ad6b1-7705-406f-ba05-d7f46ca14966.war:
/WEB-INF/
/WEB-INF/classes/
/WEB-INF/classes/test.properties
I know that there is a configuration file called web.xml
What I want to achieve is have another configuration file that has application specific configuration and it has to be read when the web server is started. I also want a Class to be able to read this configuration. Is there a way I can configure this is web.xml file itself or is there another way
You can use the Apache Commons Configuration. Have a look at the user guide. Since you want it to be done on startup here is a sample ServletContextListener:
package test;
import java.io.File;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
public class ConfigurationListener implements ServletContextListener {
#Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext context = sce.getServletContext();
File configFile;
try {
configFile = new File(context.getResource("/WEB-INF/configuration.xml").getPath());
Configuration config = new XMLConfiguration(configFile);
context.setAttribute("configuration", config);
} catch (ConfigurationException | MalformedURLException ex) {
Logger.getLogger(ConfigurationListener.class.getName()).log(Level.SEVERE, null, ex);
}
}
#Override
public void contextDestroyed(ServletContextEvent sce) {}
}
Now get your configuration anywhere in your web application like this:
Configuration config = (Configuration) request.getServletContext().getAttribute("configuration");
I would create a class to hold the configuration though rather than adding it as an attribute to the ServletContext. The class would simply provide access to the configuration through a static method.