// Path: try_java_ee7-web/src/main/java/enterprisegeeks/entity/Account.java
// @Vetoed //CDI対象外
// @Entity
// @NamedQueries({
//     @NamedQuery(name = "Account.nameOrEmali", query = "select a from Account a where a.name = :name or a.email = :email")
//     , @NamedQuery(name = "Account.byToken", query="select a from Account a where a.token = :token")}
// )
// @Table(uniqueConstraints = @UniqueConstraint(columnNames = {"name", "email"}))
// public class Account implements Serializable{
//     
//     @NotNull(message = "{required}")
//     @Size(message = "{required}", min = 1) // 未入力の場合、空文字になるため。
//     @Id
//     @Column(length = 40)
//     private String name;
//     
//     @Email(message = "{email.invalid}")
//     @NotNull(message = "{required}")
//     @Size(message = "{required}", min = 1)
//     @Column(length = 200, nullable = false)
//     private String email;
//     
//     @Column(length = 32, nullable = true )
//     private String gravaterHash;
//     
//     @Column(length = 40, nullable = true )
//     private String token;
//     
//     public String getName() {
//         return name;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// 
//     public String getEmail() {
//         return email;
//     }
// 
//     public void setEmail(String email) {
//         this.email = email;
//     }
// 
//     public String getGravaterHash() {
//         return gravaterHash;
//     }
// 
//     public void setGravaterHash(String gravaterHash) {
//         this.gravaterHash = gravaterHash;
//     }
//     
//     public String getGravatar() {
//         return "https://www.gravatar.com/avatar/" + gravaterHash;
//     }
// 
//     public String getToken() {
//         return token;
//     }
// 
//     public void setToken(String token) {
//         this.token = token;
//     }
// }
// 
// Path: try_java_ee7-web/src/main/java/enterprisegeeks/rest/dto/Authed.java
// @RequestScoped
// public class Authed {
//     
//     private Account account;
// 
//     public Account getAccount() {
//         return account;
//     }
// 
//     public void setAccount(Account account) {
//         this.account = account;
//     }
//     
//     
// }
// 
// Path: try_java_ee7-web/src/main/java/enterprisegeeks/service/Service.java
// public interface Service{
//     
//     
//     public boolean registerAccount(Account accout);
//     
//     public Account findAccountByName(String name);
//     
//     /** ログイン用トークン発行 */
//     public String publishToken(Account account);
//     
//     /** チャットルーム一覧 */
//     public List<Room> allRooms();
//     
//     /** チャットルームのチャット一覧を取得 */
//     public List<Chat> findChatByRoom(Room room, Timestamp from);
//     
//     public void addChat(Chat chat);
//     
//     public Account getAccountByToken(String token);
//     
//     
// }


import enterprisegeeks.rest.anotation.MessageResource;
import enterprisegeeks.rest.anotation.WithAuth;
import enterprisegeeks.entity.Account;
import enterprisegeeks.rest.dto.Authed;
import enterprisegeeks.service.Service;
import java.io.Serializable;
import java.util.ResourceBundle;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import javax.persistence.EntityNotFoundException;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package enterprisegeeks.rest;


/**
 *
 * 認証チェックインターセプター
 */
@Interceptor
@Dependent
@WithAuth
@Transactional
public class WithAuthInterceptor implements Serializable{

    @Inject
    private HttpServletRequest req;
    
    @Inject
    private Service service;
    
    @Inject @MessageResource
    private ResourceBundle message;
    
    @Inject
    private Authed authed;
    
    
    @AroundInvoke
    public Object invoke(InvocationContext ic) throws Exception{
        if (req == null) {
            forbidden();
        }
        
        String token = req.getHeader("authorization");
        if (token == null) {
            forbidden();
        }
// Path: try_java_ee7-web/src/main/java/enterprisegeeks/entity/Account.java
// @Vetoed //CDI対象外
// @Entity
// @NamedQueries({
//     @NamedQuery(name = "Account.nameOrEmali", query = "select a from Account a where a.name = :name or a.email = :email")
//     , @NamedQuery(name = "Account.byToken", query="select a from Account a where a.token = :token")}
// )
// @Table(uniqueConstraints = @UniqueConstraint(columnNames = {"name", "email"}))
// public class Account implements Serializable{
//     
//     @NotNull(message = "{required}")
//     @Size(message = "{required}", min = 1) // 未入力の場合、空文字になるため。
//     @Id
//     @Column(length = 40)
//     private String name;
//     
//     @Email(message = "{email.invalid}")
//     @NotNull(message = "{required}")
//     @Size(message = "{required}", min = 1)
//     @Column(length = 200, nullable = false)
//     private String email;
//     
//     @Column(length = 32, nullable = true )
//     private String gravaterHash;
//     
//     @Column(length = 40, nullable = true )
//     private String token;
//     
//     public String getName() {
//         return name;
//     }
// 
//     public void setName(String name) {
//         this.name = name;
//     }
// 
//     public String getEmail() {
//         return email;
//     }
// 
//     public void setEmail(String email) {
//         this.email = email;
//     }
// 
//     public String getGravaterHash() {
//         return gravaterHash;
//     }
// 
//     public void setGravaterHash(String gravaterHash) {
//         this.gravaterHash = gravaterHash;
//     }
//     
//     public String getGravatar() {
//         return "https://www.gravatar.com/avatar/" + gravaterHash;
//     }
// 
//     public String getToken() {
//         return token;
//     }
// 
//     public void setToken(String token) {
//         this.token = token;
//     }
// }
// 
// Path: try_java_ee7-web/src/main/java/enterprisegeeks/rest/dto/Authed.java
// @RequestScoped
// public class Authed {
//     
//     private Account account;
// 
//     public Account getAccount() {
//         return account;
//     }
// 
//     public void setAccount(Account account) {
//         this.account = account;
//     }
//     
//     
// }
// 
// Path: try_java_ee7-web/src/main/java/enterprisegeeks/service/Service.java
// public interface Service{
//     
//     
//     public boolean registerAccount(Account accout);
//     
//     public Account findAccountByName(String name);
//     
//     /** ログイン用トークン発行 */
//     public String publishToken(Account account);
//     
//     /** チャットルーム一覧 */
//     public List<Room> allRooms();
//     
//     /** チャットルームのチャット一覧を取得 */
//     public List<Chat> findChatByRoom(Room room, Timestamp from);
//     
//     public void addChat(Chat chat);
//     
//     public Account getAccountByToken(String token);
//     
//     
// }


// Path: try_java_ee7-web/src/main/java/enterprisegeeks/rest/WithAuthInterceptor.java
import enterprisegeeks.rest.anotation.MessageResource;
import enterprisegeeks.rest.anotation.WithAuth;
import enterprisegeeks.entity.Account;
import enterprisegeeks.rest.dto.Authed;
import enterprisegeeks.service.Service;
import java.io.Serializable;
import java.util.ResourceBundle;
import javax.enterprise.context.Dependent;
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import javax.persistence.EntityNotFoundException;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package enterprisegeeks.rest;


/**
 *
 * 認証チェックインターセプター
 */
@Interceptor
@Dependent
@WithAuth
@Transactional
public class WithAuthInterceptor implements Serializable{

    @Inject
    private HttpServletRequest req;
    
    @Inject
    private Service service;
    
    @Inject @MessageResource
    private ResourceBundle message;
    
    @Inject
    private Authed authed;
    
    
    @AroundInvoke
    public Object invoke(InvocationContext ic) throws Exception{
        if (req == null) {
            forbidden();
        }
        
        String token = req.getHeader("authorization");
        if (token == null) {
            forbidden();
        }
        Account account = null;