Thursday, August 8, 2013

Running an Apollo Broker Instance (from Apollo website)

Running an Apollo Broker Instance (from Apollo website)

Assuming you created the broker instance under /var/lib/mybroker all you need to do start running the broker instance is execute:
/var/lib/mybroker/bin/apollo-broker run
Now that the broker is running, you can optionally run some of the included examples to  verify the the broker is running properly.

Web Administration

Apollo provides a simple web interface to monitor the status of the broker. Once the admin interface will be accessible at:
The default login id and password is admin and password.

Saturday, June 1, 2013

Some code borrowed from BalusC.

//ServletCode for CSV Export
public static <T> void writeCsv (List<T> csv, char separator, OutputStream output) throws IOException {
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(output, "UTF-8"));
    System.out.println("Instance NextType: "+csv.getClass().getCanonicalName()+ " Size: "+ csv.size());
    //add headers here
        writer.append("ID").append(separator);
     
        writer.append("LOCATION").append(separator);
     
        writer.append("NAME").append(separator);
     
        writer.append("TYPE").append(separator);
     
        writer.append("FLAG").append(separator);
     
        writer.append("TIME1").append(separator);
     
        writer.append("SYSTIME").append(separator);
     
        writer.append("USERID");
        writer.newLine();

    for (T row : csv) {
            if(row instanceof entities.Item){
                Item item = (entities.Item)row;
                Integer itemid = item.getId();
                writer.append(itemid.toString()).append(separator);
                Date enteredtime = item.getTime1();
                if(time1 == null) {
                    writer.append(" ").append(separator);
                }
                else
                    writer.append(enteredtime1.toString()).append(separator);
                long userid = item.getUserid();
                writer.append(String.valueOf(userid));
.....
            }

        writer.newLine();
    }
    writer.flush();
}

    @EJB
    private beans.ItemFacade ejbFacade;
 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        List<Item> listOfObj = new ArrayList<Item>();

        listOfObj = ejbFacade.findAll();
        List<Item> csvList = new ArrayList<Item>();
    List<Item> csv = listOfObj;
    response.setHeader("Content-Type", "text/csv");
    response.setHeader("Content-Disposition", "attachment;filename=\"file"+System.currentTimeMillis()+".csv\"");
    writeCsv(csv, ',', response.getOutputStream());
    }

Wednesday, February 6, 2013

JSF Logged Interceptor


Logged JavaEE Interceptor for JSF logging

import java.io.Serializable;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

/**
 *
 * @author happyFace
 */
@Logged
@Interceptor
public class LoggedInterceptor implements Serializable {

    public static final Logger LOGGER = Logger.getLogger(LoggedInterceptor.class.getName());
    public LoggedInterceptor() {
    }

    @AroundInvoke
    public Object logMethodEntry(InvocationContext invocationContext)
            throws Exception {
        LOGGER.log(Level.INFO, "Entering method: {0} in class {1} at Time {2}.",
                new Object[]{invocationContext.getMethod().getName(),
                    invocationContext.getMethod().getDeclaringClass().getName(),
                    new Date()});
        return invocationContext.proceed();
    }
//}  
}
---------------------------------------------------------------------------


import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.interceptor.InterceptorBinding;

/**
 *
 * @author happyFace
 */

@Inherited
@InterceptorBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface Logged {
}


--------------------------------------------------------------------------------------
import java.security.Principal;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import javax.interceptor.Interceptors;

/**
 *
 * @author happyFace
 */
@Named
@ViewScoped
@Interceptors({ LoggedInterceptor.class})
public class Index {
    private String name = "Get A Lawyer";

    /**
     * @return the name
     */
    public String getName() {
        return name;
    }

    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    
    public String logout(){
        Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
        System.out.println("Principal: "+userPrincipal);
        return "login?faces-redirect=true";
    }
    
}

JSF Managed Bean Session Logout

Session Logout Code For JSF With Faces-Redirect


From http://stackoverflow.com/questions/2206911/best-way-for-user-authentication-on-javaee-6-using-jsf-2-0

<h:form>
    <h:outputLabel for="username" value="Username" />
    <h:inputText id="username" value="#{auth.username}" required="true" />
    <h:message for="username" />
    <br />
    <h:outputLabel for="password" value="Password" />
    <h:inputSecret id="password" value="#{auth.password}" required="true" />
    <h:message for="password" />
    <br />
    <h:commandButton value="Login" action="#{auth.login}" />
    <h:messages globalOnly="true" />
</h:form>



@ManagedBean
@ViewScoped
public class Auth {

    private String username;
    private String password;
    private String originalURL;

    @PostConstruct
    public void init() {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        originalURL = (String) externalContext.getRequestMap().get(RequestDispatcher.FORWARD_REQUEST_URI);

        if (originalURL == null) {
            originalURL = externalContext.getRequestContextPath() + "/home.xhtml";
        } else {
            String originalQuery = (String) externalContext.getRequestMap().get(RequestDispatcher.FORWARD_QUERY_STRING);

            if (originalQuery != null) {
                originalURI += "?" + originalQuery;
            }
        }
    }

    @EJB
    private UserService userService;

    public void login() throws IOException {
        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext externalContext = context.getExternalContext();
        HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();

        try {
            request.login(username, password);
            User user = userService.find(username, password);
            externalContext.getSessionMap().put("user", user);
            externalContext.redirect(originalURL);
        } catch (ServletException e) {
            // Handle unknown username/password in request.login().
            context.addMessage(null, new FacesMessage("Unknown login"));
        }
    }

    public void logout() throws IOException {
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        externalContext.invalidateSession();
        externalContext.redirect(externalContext.getRequestContextPath() + "/login.xhtml");
    }

    // Getters/setters for username and password.
}




public String logout() {
    FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
    return "login?faces-redirect=true";
}


This is for getting the principal

FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();

JSF CommandLink JQuery Call To BootStrap Modal

JSF h:commandLink with JQuery onclick BootStrap modal.

<h:commandLink onclick="$('#myModal1').modal('show'); return false;" value="Launch Modal"/>


Modal

<br />
<div class="modal hide fade" id="Modal" style="display: none;">
<div class="modal-header">
<a class="close" data-dismiss="modal" href="http://www.blogger.com/blogger.g?blogID=5930143319420917182#">×</a>
              <br />
<h3>
Action!</h3>
</div>
<div class="modal-body">
Item Name: #{item.name}<br />
Item Location: #{item.name2}</div>
<div class="modal-footer">
<a class="btn" data-dismiss="modal" href="#">Cancel</a>
           
              <h:commandlink action="#{bean.method}" styleclass="btn btn-danger btn-small" value="Delete">
            </h:commandlink></div>
</div>