Skip to content
This repository has been archived by the owner on Dec 5, 2019. It is now read-only.

Agpush 503 #43

Merged
merged 11 commits into from Aug 20, 2014
25 changes: 20 additions & 5 deletions pom.xml
Expand Up @@ -56,11 +56,6 @@
<version>3.5.6-Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>4.5.1.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
Expand Down Expand Up @@ -208,6 +203,26 @@
</build>

<profiles>
<profile>
<id>jboss7</id>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search</artifactId>
<version>4.3.0.Final</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>wildfly</id>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>4.5.1.Final</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>jbossas</id>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want to activate one profile, on default ? e.g. wildfly ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default profile is used for the arquillian tests

<activation>
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Expand Up @@ -79,7 +79,9 @@ For all these aliases the password is ``` 123 ```

# Deploying the app

``` mvn clean install jboss-as:deploy ```
``` mvn clean install jboss-as:deploy -Pwildfly ```

Use the wildfly profile to build and deploy to wildfly and jboss7 for jboss as 7.

There is also a lucene index created with the location of sales agents default location is '.' you can change to a more permanent location in the persistence.xml

Expand Down
97 changes: 16 additions & 81 deletions src/main/java/org/jboss/aerogear/aerodoc/rest/LeadEndpoint.java
Expand Up @@ -16,35 +16,22 @@
*/
package org.jboss.aerogear.aerodoc.rest;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import org.jboss.aerogear.aerodoc.config.RequiresAccount;
import org.jboss.aerogear.aerodoc.model.Lead;
import org.jboss.aerogear.aerodoc.service.LeadSender;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.*;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriBuilder;

import org.jboss.aerogear.aerodoc.config.RequiresAccount;
import org.jboss.aerogear.aerodoc.model.Lead;
import org.jboss.aerogear.aerodoc.service.LeadSender;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
*
Expand All @@ -60,22 +47,6 @@ public class LeadEndpoint {
@PersistenceContext
private EntityManager em;

@OPTIONS
public Response crossOriginForInstallations(@Context HttpHeaders headers) {
return appendPreflightResponseHeaders(headers, Response.ok()).build();
}

@OPTIONS
@Path("/{token}")
public Response crossOriginForLead(@Context HttpHeaders headers) {
return appendPreflightResponseHeaders(headers, Response.ok()).build();
}

@OPTIONS
public Response crossOriginForLeads(@Context HttpHeaders headers) {
return appendPreflightResponseHeaders(headers, Response.ok()).build();
}

@POST
@Consumes("application/json")
@RequiresAccount
Expand All @@ -102,84 +73,48 @@ public Response deleteById(@PathParam("id") Long id) {
@Path("/{id:[0-9][0-9]*}")
@Produces("application/json")
@RequiresAccount
public Response findById(@PathParam("id") Long id, @Context HttpServletRequest request) {
TypedQuery<Lead> findByIdQuery = em
.createQuery(
"SELECT l FROM Lead l WHERE l.id = :entityId",
Lead.class);
public Response findById(@PathParam("id") Long id) {
TypedQuery<Lead> findByIdQuery = em.createQuery("SELECT l FROM Lead l WHERE l.id = :entityId", Lead.class);
findByIdQuery.setParameter("entityId", id);
Lead entity = findByIdQuery.getSingleResult();
if (entity == null) {
return Response.status(Status.NOT_FOUND).build();
}
return appendAllowOriginHeader(Response.ok(entity), request);
return Response.ok(entity).build();
}

@GET
@Produces("application/json")
@RequiresAccount
public Response listAll(@Context HttpServletRequest request) {
final List<Lead> results = em.createQuery(
"SELECT l FROM Lead l where l.saleAgent = null", Lead.class)
public Response listAll() {
final List<Lead> results = em.createQuery("SELECT l FROM Lead l where l.saleAgent = null", Lead.class)
.getResultList();
return appendAllowOriginHeader(Response.ok(results), request);
return Response.ok(results).build();
}

@PUT
@Path("/{id:[0-9][0-9]*}")
@Consumes("application/json")
@RequiresAccount
public Response update(@PathParam("id") Long id, Lead entity, @Context HttpServletRequest request) {
public Response update(@PathParam("id") Long id, Lead entity) {
entity.setId(id);
entity = em.merge(entity);
//Broadcast the change to everyone
leadSender.sendBroadCast(entity);
return appendAllowOriginHeader(Response.noContent(), request);
return Response.noContent().build();
}

@POST
@Path("/sendleads/{id:[0-9][0-9]*}")
@RequiresAccount
public void sendLead(@PathParam("id") Long id, List<LinkedHashMap> agents) {
TypedQuery<Lead> findByIdQuery = em
.createQuery(
"SELECT l FROM Lead l WHERE l.id = :entityId",
Lead.class);
TypedQuery<Lead> findByIdQuery = em.createQuery("SELECT l FROM Lead l WHERE l.id = :entityId", Lead.class);
findByIdQuery.setParameter("entityId", id);
Lead entity = findByIdQuery.getSingleResult();
List<String> aliases = new ArrayList<String>();
for (LinkedHashMap hashMap : agents) {
aliases.add(hashMap.get("loginName").toString());

}
leadSender.sendLeads(aliases, entity);
}

protected ResponseBuilder appendPreflightResponseHeaders(HttpHeaders headers,
ResponseBuilder response) {
// add response headers for the preflight request
// required
response.header("Access-Control-Allow-Origin",
headers.getRequestHeader("Origin").get(0))
.header("Access-Control-Allow-Methods",
"POST,DELETE,GET,PUT")
.header("Access-Control-Allow-Headers",
"accept, origin, content-type, authorization")
.header("Access-Control-Allow-Credentials", "true");

return response;
}

/**
* This convenient method will append to the response headers the needed
* CORS headers
*/
protected Response appendAllowOriginHeader(ResponseBuilder rb,
HttpServletRequest request) {

return rb
.header("Access-Control-Allow-Origin",
request.getHeader("Origin")) // return submitted origin
.header("Access-Control-Allow-Credentials", "true").build();
}
}
56 changes: 20 additions & 36 deletions src/main/java/org/jboss/aerogear/aerodoc/rest/Login.java
Expand Up @@ -22,34 +22,23 @@
import org.picketlink.Identity.AuthenticationResult;
import org.picketlink.credential.DefaultLoginCredentials;
import org.picketlink.idm.IdentityManager;
import org.picketlink.idm.model.IdentityType;
import org.picketlink.idm.model.basic.Agent;
import org.picketlink.idm.model.basic.BasicModel;
import org.picketlink.idm.model.basic.User;
import org.picketlink.idm.query.IdentityQuery;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import java.util.List;
import java.util.logging.Logger;

@Stateless
@Path("/")
public class Login {

private static final Logger LOGGER = Logger.getLogger(Login.class
.getSimpleName());
private static final Logger LOGGER = Logger.getLogger(Login.class.getSimpleName());

@Inject
private IdentityManager identityManager;
Expand All @@ -64,28 +53,26 @@ public class Login {
@Path("/login")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response login(final SaleAgent user,
@Context HttpServletRequest request) {
SaleAgent saleAgent = null;
String id = user.getLoginName();
if (!this.identity.isLoggedIn()) {
this.credentials.setUserId(user.getLoginName());
this.credentials.setPassword(user.getPassword());
AuthenticationResult result = this.identity.login();
LOGGER.info("Login result : " + result);
if(result==AuthenticationResult.SUCCESS){
List<SaleAgent> list = identityManager.createIdentityQuery(SaleAgent.class)
.setParameter(SaleAgent.LOGIN_NAME, user.getLoginName()).getResultList();
saleAgent = list.get(0);
}
else {
LOGGER.severe("Login failed !");
}
} else {
throw new RuntimeException("Authentication failed");
}
public Response login(final SaleAgent user) {
if (!identity.isLoggedIn()) {
credentials.setUserId(user.getLoginName());
credentials.setPassword(user.getPassword());

AuthenticationResult result = identity.login();
LOGGER.info("Login result : " + result);

if (result == AuthenticationResult.SUCCESS){
List<SaleAgent> list = identityManager.createIdentityQuery(SaleAgent.class)
.setParameter(SaleAgent.LOGIN_NAME, user.getLoginName()).getResultList();
return Response.ok(list.get(0)).build();
} else {
LOGGER.severe("Login failed !");
}
} else {
throw new RuntimeException("Authentication failed");
}

return Response.ok(saleAgent).build();
return Response.status(Response.Status.FORBIDDEN).build();
}

@POST
Expand All @@ -95,7 +82,4 @@ public void logout() {
LOGGER.info("User logout!");
identity.logout();
}



}
1 change: 1 addition & 0 deletions src/main/webapp/scripts/services/dataService.js
Expand Up @@ -23,6 +23,7 @@ aerodoc.factory("dataService", function() {
name : "auth",
settings : {
agAuth : true,
contentType : "application/json",
endpoints : {
enroll : endpoint + "register",
login : endpoint + "login",
Expand Down
Expand Up @@ -50,15 +50,14 @@ public class SaleAgentEndpointTest {
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class, "test.war")
.addPackage(SalesAgentEntity.class.getPackage())
.addClass(AerodocBaseEndpoint.class)
.addClass(SaleAgentEndpoint.class)
.addClass(SaleAgent.class)
.addAsLibraries(
Maven.resolver()
.loadPomFromFile("pom.xml").resolve(
"org.picketlink:picketlink-impl:2.5.2.Final",
"org.picketlink:picketlink-idm-simple-schema:2.5.2.Final",
"org.hibernate:hibernate-search"
.resolve(
"org.picketlink:picketlink:2.6.1.Final",
"org.picketlink:picketlink-idm-simple-schema:2.6.1.Final",
"org.hibernate:hibernate-search:4.3.0.Final"
).withTransitivity().asFile()
)
.addAsResource("persistence-test.xml", "META-INF/persistence.xml")
Expand Down