How to call a JavaEE REST web service with BASIC Authentication using jquery ajax
Tech-Today

How to call a JavaEE REST web service with BASIC Authentication using jquery ajax


I don't really remember when I coded it, nor where I got it but I'm writing it here for future use :-)
Below is the code I use to test CORS, http://en.wikipedia.org/wiki/Cross-origin_resource_sharing.
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
var $ = jQuery.noConflict();
 
$.aja(angry) {
cache: false,
crossDomain: true,
dataType: "json",
url: "http://czetsuya/myService/meMethod",
type: "GET",
success: function( jsonObj, textStatus, xhr ) {
var htmlContent = $( "#logMsgDiv" ).html( ) + "<p>" + jsonObj.message + "</p>";
$( "#logMsgDiv" ).html( htmlContent );
},
beforeSend: function (xhr) {
xhr.setRequestHeader ("Authorization", "Basic " + btoa("username:password"));
},
error: function( xhr, textStatus, errorThrown ) {
console.log( "HTTP Status: " + xhr.status );
console.log( "Error textStatus: " + textStatus );
console.log( "Error thrown: " + errorThrown );
}
} );
</script>

And here are the javaEE filters.
import java.io.IOException;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author Edward P. Legaspi
**/
@Provider
@PreMatching
public class RESTCorsRequestFilter implements ContainerRequestFilter {

private final static Logger log = LoggerFactory
.getLogger(RESTCorsRequestFilter.class.getName());

@Override
public void filter(ContainerRequestContext requestCtx) throws IOException {
// When HttpMethod comes as OPTIONS, just acknowledge that it accepts...
if (requestCtx.getRequest().getMethod().equals("OPTIONS")) {
log.debug("HTTP Method (OPTIONS) - Detected!");

// Just send a OK signal back to the browser
requestCtx.abortWith(Response.status(Response.Status.OK).build());
}
}

}

import java.io.IOException;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.ext.Provider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author Edward P. Legaspi
**/
@Provider
@PreMatching
public class RESTCorsResponseFilter implements ContainerResponseFilter {
private final static Logger log = LoggerFactory
.getLogger(RESTCorsResponseFilter.class.getName());

@Override
public void filter(ContainerRequestContext requestCtx,
ContainerResponseContext responseCtx) throws IOException {
log.debug("Adding CORS to the response.");

responseCtx.getHeaders().add("Access-Control-Allow-Origin", "*");
responseCtx.getHeaders()
.add("Access-Control-Allow-Credentials", "true");
responseCtx.getHeaders().add("Access-Control-Allow-Methods",
"GET, POST, DELETE, PUT");
}

}

If you are using RESTEasy just like I'm usually am. You can take advantage of the already available CorsFilter class:
package com.weddinghighway.api.rest.filter;

import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.plugins.interceptors.CorsFilter;

/**
* @author Edward P. Legaspi
* @created 5 Oct 2017
*/

@Provider
public class RESTCorsResponseFilter implements Feature {

@Override
public boolean configure(FeatureContext context) {
CorsFilter corsFilter = new CorsFilter();
corsFilter.getAllowedOrigins().add("*");
context.register(corsFilter);
return true;
}
}

Note: If one fails, then just try the other :-)




- Social Login Using Rest Fb
This is an implementation tutorial on how we can use REST FB to enable facebook social login on our web application. Basically, it's a project created from javaee7-war template. To run this app you need to set up a Facebook application with callback...

- How To Protect Your Page Using Webfilter In Javaee
This tutorial is to be use in conjunction with picketlink. Normally we want some pages to be accessible only after a user has logged in. In this case we need a real protection filter. The class below filters a url path and check if there's a logged...

- Jax-rs 2.0 Security Tutorial In Javaee And Jboss
This tutorial will summarize how the author was able to call a secured rest webservice using resteasy. We will not go into detail on how we build the entire project since the code is already pushed at github. Basically we will just note down the most...

- Loading Locale From Post/get Parameter
This page will summarize how to load a resource bundle dynamically from a GET/POST parameter.  1.) In your mail xhtml template, make sure that you define f:view locale before the h:body tag: <f:view locale="#{languageBean.currentLocale}" />...

- How To Receive An Asynchronous Jms Message
This tutorial assumes that you have glassfish installed and followed the 2 previous tutorials: http://czetsuya-tech.blogspot.com/2012/06/how-to-setup-set-jms-factory-and-queue.html http://czetsuya-tech.blogspot.com/2012/06/how-to-send-jms-message-to-jms-queue.html...



Tech-Today








.