I have a schedule service which gets its job via REST as JSON.
The Resource class:
import java.io.IOException;
import javax.inject.Inject;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;
import de.fszm.scheduler.controller.SchedulerController;
import de.fszm.scheduler.entities.MySchedule;
#Path("/schedule")
public class ScheduleRESTResource {
#Inject
SchedulerController scheduleController;
#POST
#Path("/job")
#Consumes(MediaType.APPLICATION_JSON)
public void schedule(MySchedule schedule) throws IOException {
scheduleController.buildSchedule(schedule);
}
}
The ScheduleController:
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import de.fszm.scheduler.entities.MySchedule;
#Named
#RequestScoped
public class SchedulerController {
public void buildSchedule(MySchedule schedule) {
System.out.println("Test");
}
}
The SchedulerMain:
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.wildfly.swarm.container.Container;
import org.wildfly.swarm.jaxrs.JAXRSArchive;
import de.fszm.scheduler.controller.SchedulerController;
import de.fszm.scheduler.entities.MySchedule;
import de.fszm.scheduler.rest.JaxRSActivator;
import de.fszm.scheduler.rest.ScheduleRESTResource;
public class SchedulerMain {
public static void main(String[] args) {
try {
Container container = new Container();
container.start();
JAXRSArchive appDeployment = ShrinkWrap.create(JAXRSArchive.class);
appDeployment.addResource(ScheduleRESTResource.class);
appDeployment.addResource(JaxRSActivator.class);
appDeployment.addClass(MySchedule.class);
appDeployment.addClass(SchedulerController.class);
appDeployment.addAllDependencies();
container.deploy(appDeployment);
} catch (Exception e) {
e.printStackTrace();
}
}
}
And the MySchedule ist just a POJO
My problem is that when I POST JSON to the I get a NullPointerException for the injected SchedulerController.
I also have a beans.xml in src/main/webapp/WEB-INF and in my pom.xml I have the following dependency for CDI (weld)
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>weld</artifactId>
</dependency>
and
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>1.2</version>
</dependency>
I am using WildFly swarm 1.0.0 beta 6
What did I miss?
I think you are missing the Swarm CDI fraction.
Try adding this to your dependencies instead.
<dependency>
<groupId>org.wildfly.swarm</groupId>
<artifactId>cdi</artifactId>
</dependency>
Related
I can run an apache-storm program in local mode (using Intellij and Maven), but when I run on the storm cluster it bombs immediately with the error "Exception in thread “main” java.lang.NoClassDefFoundException: org.apache.http.client.HttpClient". I have verified the storm cluster works on a toy program with no http calls. Here is my pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.jmm</groupId>
<artifactId>twitter2</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.apache.storm</groupId>
<artifactId>storm-core</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20090211</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
And the start of my spout code:
import org.apache.storm.spout.SpoutOutputCollector;
import org.apache.storm.task.TopologyContext;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseRichSpout;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;
import org.apache.storm.utils.Utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import twitter4j.Status;
public class twitterSpout extends BaseRichSpout {
private SpoutOutputCollector collector;
String bearerToken = ""; //deleted here, but filled in for my program
HttpGet httpGet;
HttpResponse response;
HttpEntity entity;
URIBuilder uriBuilder;
And the topology:
import org.apache.storm.Config;
import org.apache.storm.StormSubmitter;
import org.apache.storm.topology.TopologyBuilder;
import org.apache.storm.tuple.Fields;
import org.apache.storm.LocalCluster;
public class twitterTopology {
public static void main(String[] args) throws Exception {
twitterSpout spout = new twitterSpout();
System.out.println("\nafter initializing twitterSpout\n");
LossyBolt countBolt = new LossyBolt();
ReportBolt reportBolt = new ReportBolt();
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("twitterSpout", spout);
builder.setBolt("lossy", countBolt).fieldsGrouping("twitterSpout", new Fields("bucket"));
builder.setBolt("report", reportBolt).fieldsGrouping("lossy", new Fields("bcurrent"));
Config config = new Config();
config.setDebug(true);
config.setNumWorkers(3);
/* this code works when I uncomment it and comment out StormSubmitter
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("twitterTopology", config, builder.createTopology());
Thread.sleep(70000);
cluster.killTopology("twitterTopology");;
cluster.shutdown();
*/
StormSubmitter.submitTopology("twitterTopology", config, builder.createTopology());
Thread.sleep(70000);
}
}
Any insight as to why it works in local mode but not in storm cluster mode would be appreciated!!
I am pretty new to spring-boot, apache camel and ActiveMQ broker. I am trying to create an application which will send a message to a queue which I am hosting locally using Camel for routing.
When I run the app, I get the error :
ERROR org.apache.camel.FailedToCreateRouteException: Failed to create route route1 at: >>> To[activemq:queue:myQueue] <<< in route: Route(route1)[[From[direct:firstRoute]] -> [SetBody[constant... because of Failed to resolve endpoint: activemq://queue:myQueue due to: No component found with scheme: activemq
POM:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>2.22.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.22.0</version>
</dependency>
MsgRouteBuilder:
public void configure() throws Exception {
from("direct:firstRoute")
.setBody(constant("Hello"))
.to("activemq:queue:myQueue");
}
application.yaml:
activemq:
broker-url: tcp://localhost:61616
user: meAd
password: meAd
MainApp.java:
package me.ad.myCamel;
import org.apache.camel.CamelContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import me.ad.myCamel.router.MessageRouteBuilder;
#SpringBootApplication
#EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
#EnableAspectJAutoProxy(proxyTargetClass = true)
#EnableCaching
public class MeAdApp implements CommandLineRunner {
private static final Logger LOG = LoggerFactory.getLogger(MeAdApp.class);
#Autowired
private CamelContext camelContext;
public static void main(String[] args) {
try {
SpringApplication.run(MeAdApp.class, args);
} catch (Exception ex) {
LOG.error(ex.getMessage(), ex);
}
}
#Override
public void run(String... args) throws Exception {
LOG.info("Starting MeAdApp...");
}
#Bean
public MsgRouteBuilder msgRouteBuilder() throws Exception {
MsgRouteBuilder routeBuilder = new MsgRouteBuilder();
camelContext.addRoutes(routeBuilder);
return routeBuilder;
}
}
Can anybody please point me to the right direction as to why I am getting this error ? Any help is greatly appreciated .
You need to add camel-activemq in the POM.xml file
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-activemq</artifactId>
<version>3.2.0</version>
</dependency>
Your application doesn't know the component activemq. To resolve that, you need to add the dependency of camel-activemq in your pom.xml file :
<properties>
...
<activemq.version>3.1.0</activemq.version>
</properties>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-activemq</artifactId>
<version>${activemq.version}</version>
</dependency>
I am trying to apply authentication to a few methods of rest services. So apply the annotation #NameBinding for reference to the class of the filter that will validate the authentication, but when doing the test the service passes the filter (it never enters to validate) showing "El msn es...", when it should return 401 Unauthorized .
NameBinding class:
package api.movil.token;
import javax.ws.rs.NameBinding;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
#Retention(value = RUNTIME)
#Target({TYPE, METHOD})
#NameBinding
public #interface JWTTokenNeeded {
}
Filter class:
package api.movil.token;
import io.jsonwebtoken.Jwts;
import javax.annotation.Priority;
import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import api.movil.util.KeyGenerator;
import java.io.IOException;
import java.security.Key;
#Provider
#Priority(Priorities.AUTHENTICATION)
#JWTTokenNeeded
public class JWTTokenNeededFilter implements ContainerRequestFilter {
public void filter(ContainerRequestContext requestContext) throws IOException {
System.out.println("entro --------------->");
String authorizationHeader = requestContext.getHeaderString(HttpHeaders.AUTHORIZATION);
System.out.println("#### authorizationHeader : " + authorizationHeader);
if (authorizationHeader == null || !authorizationHeader.startsWith("Bearer ")) {
System.out.println("#### invalid authorizationHeader : " + authorizationHeader);
throw new NotAuthorizedException("Authorization header must be provided");
}
String token = authorizationHeader.substring("Bearer".length()).trim();
try {
// Validate the token
KeyGenerator keyGenerator=new KeyGenerator();
Key key = keyGenerator.generateKey();
Jwts.parser().setSigningKey(key).parseClaimsJws(token);
System.out.println("#### valid token : " + token);
} catch (Exception e) {
System.out.println("#### invalid token : " + token);
requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).build());
}
}
}
Rest service class:
package api.movil.servicios;
#Path("/users")
public class UserEndpoint {
#POST
#Path("/getPrueba")
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
#JWTTokenNeeded
public Response getPrueba(#FormParam("msn")String msn){
System.out.println(msn);
return Response.ok("El msn es "+msn).build();
}
}
Class of configuration:
package api.movil.servicios;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import api.movil.token.JWTTokenNeededFilter;
import api.movil.util.ExceptionHandler;
#ApplicationPath("rest")
public class ApplicationConfigura extends Application {
public Set<Class<?>> getClasses() {
return getRestClasses();
}
//Auto-generated from RESTful web service wizard
private Set<Class<?>> getRestClasses() {
Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
resources.add(UserEndpoint.class);
resources.add(JWTTokenNeededFilter.class);
resources.add(ExceptionHandler.class);
return resources;
}
}
pom.xml:
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.17</version>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.7.0</version>
</dependency>
</dependencies>
I am using the weblogic application server.
I have a few endpoints under "/rest/role/"whatever"
how do i catch bad requests for example "rest/role/dbbhwbhb/wdwdwed
if i use :
#GET
#Path("/{param}")
#Produces(MediaType.TEXT_PLAIN)
public Response badURLS() {
return Response.ok().entity("bad url").build();
}
this catches rest/roles/dfsdfsds but rest/roles/sdsasd/asdad is still not caught. How do I set up a method so if none of my endpoints are hit then return a default message ?
JBoss approach
https://docs.jboss.org/jbportal/v2.7.0.B1/referenceGuide/html/errorhandling.html
RESTeasy approach
https://docs.jboss.org/resteasy/docs/2.2.0.GA/userguide/html/ExceptionHandling.html
EDIT
As I sent to you before - second link does the trick.
Given the mapper and structure written in such way:
package atata;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
#Provider
public class _404Handler implements ExceptionMapper<NotFoundException> {
public Response toResponse(NotFoundException exception) {
return Response.status(500).entity(new ResponseEntity(333, "Got 404.")).type(MediaType.APPLICATION_JSON).build();
}
}
Config
package atata;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class EmployeeApplication extends Application {
private Set<Object> singletons = new HashSet<Object>();
public EmployeeApplication() {
singletons.add(new MessageRestService());
singletons.add(new _404Handler());
}
#Override
public Set<Object> getSingletons() {
return singletons;
}
}
Pom:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.atata</groupId>
<artifactId>atata</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>atata Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jboss.resteasy/resteasy-jaxrs -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.19.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-servlet-initializer</artifactId>
<version>3.0.4.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>3.0.14.Final</version>
</dependency>
</dependencies>
<build>
<finalName>atata</finalName>
</build>
Endpoint:
package atata;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
#Path("/roles")
public class MessageRestService {
#GET
#Path("/1")
#Produces(MediaType.APPLICATION_JSON)
public Response badURLS() {
return Response.ok().entity("OK").build();
}
}
You will get
I use maven-dependency-plugin to copy jar files of artifactItem, instead of giving artifactItem details at pom.xml as shown below
<artifactItem>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.2</version>
<overWrite>true</overWrite>
<outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
<destFileName>junit.jar</destFileName>
</artifactItem>
i am trying to extend the pluging to give artifactItems detail progamatically,by using below class
package com.nuwaza.aqua.sample.dependencymanager;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.factory.DefaultArtifactFactory;
import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.DefaultArtifactHandler;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.DefaultArtifactRepository;
import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
import org.apache.maven.artifact.repository.layout.DefaultRepositoryLayout;
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.dependency.fromConfiguration.AbstractFromConfigurationMojo;
import org.apache.maven.plugin.dependency.fromConfiguration.ArtifactItem;
import org.apache.maven.plugin.dependency.fromConfiguration.CopyMojo;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.artifact.versioning.VersionRange;
/**
*
* #id copy
* #goal copy
* #phase package
* #author anoopab
*/
public class BundleDataMojo extends CopyMojo {
#Parameter(required = true)
private List<ArtifactItem> artifactItems;
#Component
protected static ArtifactFactory factory;
public void execute() throws MojoExecutionException {
super.setArtifactItems(setArtifactDetail());
super.execute();
}
private List<ArtifactItem> setArtifactDetail() {
artifactItems = new ArrayList<ArtifactItem>();
ArtifactItem bundle = new ArtifactItem();
bundle.setGroupId("junit");
bundle.setArtifactId("junit");
bundle.setVersion("3.8.2");
bundle.setDestFileName("samplejunit.jar");
bundle.setType("jar");
bundle.setOverWrite("true");
artifactItems.add(bundle);
return artifactItems;
}
}
but i getting the following erorr
[info] Configured Artifact: junit:junit:3.8.2:jar
Exception in thread "main" java.lang.NullPointerException
at org.apache.maven.plugin.dependency.fromConfiguration.AbstractFromConfigurationMojo.getArtifact(AbstractFromConfigurationMojo.java:221)
at org.apache.maven.plugin.dependency.fromConfiguration.AbstractFromConfigurationMojo.getProcessedArtifactItems(AbstractFromConfigurationMojo.java:166)
at org.apache.maven.plugin.dependency.fromConfiguration.CopyMojo.execute(CopyMojo.java:67)
at com.nuwaza.aqua.sample.dependencymanager.BundleDataMojo.execute(BundleDataMojo.java:52)
at com.nuwaza.aqua.sample.dependencymanager.BundleDataMojo.main(BundleDataMojo.java:118)
that is
/**
* Used to look up Artifacts in the remote repository.
*/
#Component
protected ArtifactFactory factory;
this factory getting null value.....
ArtifactFactory interface at org.apache.maven.artifact.factory
I'm really confused by these. Could anyone give any help? Thanks a lot!