Basic Authentication at Grizzly Server - java

I am using Jersey Grizzly and want to implement Basic Authentication to Grizzly. I create my grizzly server as follows:
ResourceConfig rc = new PackagesResourceConfig("com.abc.de");
GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
at another class I have something like:
#GET
#Produces("text/plain")
#Path("/description")
public String getDescription() {
String description = "probeDescription";
return description;
}
I don't know the difference and main concepts of jersey and grizzly. What jersey can't do that grizzly can. However I want that if a user wants to get description it should pass the basic authentication.
Any ideas about how can I implement it?

I followed that example: http://java.net/projects/jersey/sources/svn/show/trunk/jersey/samples/https-clientserver-grizzly?rev=5752 and it works.

Related

Get JAXRS result in java, IBM

I have set up the following JAXRS service in:
#Path(value="/validationService")
public class ValidationService {
#GET
#Produces(value="text/plain")
#Path(value="{token}")
public String getPropety(#PathParam("token") String token) {
String status = AuthenticationManager.getInstance().getTokenStatus(token);
return status;
}
}
If I hit the url, I receive the proper response on my screen. How can I consume this resouce in the back end? I have set this up using jersey jars and the Cient / CLientBuilder / WebTarget procedure, using TOMCAT, but this does not seem to work with WebsphereApplicationServer 8.5. Any info is appreciated.
answer:
JAX-RS Jersey Client on Websphere 8.5
I was using the incorrect libraries to perform these operations.
I was using: javax.ws.rs to build the client, when I should've been using: org.apache.wink

Consuming a RESTful WebService passing a JSON object as request body

I've defined a RESTful WebService (by using RESTEasy on JBoss AS 7) that consumes a JSON data stream.
#PUT
#Path("/send")
#Consumes(MediaType.APPLICATION_JSON)
public Response consumeJSON(Student student) {
String output = student.toString();
// Do something...
return Response.status(200).entity(output).build();
}
How can I call my WS from another Spring-based webapp, by properly using the RestTemplate, mapping a Java Object to JSON and passing it as request body?
Note: I'm asking about Spring with the aim to investigate the facilities provided by the framework. I well know that it is possible to do that by defining manually the request body.
Cheers, V.
In the client application, you can create an interface with the same signature as the one you expose on the server side, and the same path.
Then, in the spring configuration file, you can use the RESTeasy client API to generate a proxy connecting to the exposed webservice.
In the client application, it would look like this :
SimpleClient.java
#PUT
#Path("/send")
#Consumes(MediaType.APPLICATION_JSON)
public Response consumeJSON(Student student);
Config.java
#Bean
public SimpleClient getSimpleClient(){
Client client = ClientFactory.newClient();
WebTarget target = client.target("http://example.com/base/uri");
ResteasyWebTarget rtarget = (ResteasyWebTarget)target;
SimpleClient simple = rtarget.proxy(SimpleClient.class);
return simple;
}
Then, in the place where you want to invoke this web service, you inject it with Spring and you can call the method. RESTeasy will search for the webservice matching with with your client (according to the path and the request type) and will create a connection.
Launcher.java
#Resource
private SimpleClient simpleClient;
public void sendMessage(Student student) {
simpleClient.consumeJSON(student);
}
Docs on the RESTesay client API : http://docs.jboss.org/resteasy/docs/3.0.7.Final/userguide/html/RESTEasy_Client_Framework.html
Hope this was helpfull.

How provide authenticatioon to check username and password for Soap based webservices

I am working on SOAP based web services. I want to check for credentials.
#WebService(name="Helloworld",portName="HelloworldPort", serviceName = "Helloworld", targetNamespace="http://Helloworld.com/webservices")
#SOAPBinding(style = SOAPBinding.Style.RPC, use= SOAPBinding.Use.LITERAL, parameterStyle= SOAPBinding.ParameterStyle.WRAPPED)
public class Helloworld {
public String sayHello(#WebParam(name="name")String name) {
return "hello "+name;
}
}
I have read some where that we can use USERNAME_PROPERTY, and PASSWORD_PROPERTY to services.
It seems it is Basic http authentication.
I don't know how to accommodate it in service as well as in client.
Can some one tell me how to achieve it.
Thanks.

Different web service object after each call

I am new with Java EE and SOAP. I have tried to create a simple web service application and its client (environment: NetBeans 7.2.1 IDE, GlassFish Server 3.1, Java 1.6).
Web service code:
package simplews;
import javax.jws.*;
#WebService(serviceName = "SimpleWebService")
public class SimpleWebService {
String something = null;
#WebMethod(operationName = "setSomething")
#Oneway
public void setSomething(#WebParam(name = "smth") String smth) {
something = smth;
}
#WebMethod(operationName = "getSomething")
public String getSomething() {
return something;
}
}
Client application code:
package simpleclientapp;
import simplews.*;
public class SimpleClientApp {
public static void main(String[] args) {
SimpleWebService_Service service = new SimpleWebService_Service();
SimpleWebService port = service.getSimpleWebServicePort();
port.setSomething("trololo");
String smth = port.getSomething();
System.out.println(smth);
}
}
Unfortunately, the client application printed out null. After short investigation I have realised, that on the server side a new SimpleWebService object is created for each client call (sounds like stateless approach).
What is wrong here? Why the client port does not refer to the same WS object for each call?
Web services are stateless by nature. In order to keep state between requests, you have to persist the data (in a file,database etc.).
You're right, JAX-WS web services are stateless by default and you can't rely on something thatviolates this premise. Follow a different approach in storing such values. You can read this doc Java TM API for XML Web Services (JAX-WS) Stateful Web Service with JAX-WS RI, if you really want to follow the direction in your post.

Integrate Jersey with RMI

I have a Java Jersey project, where I am running an application. On the other hand I have a project were there is a RMI application how can I put this two to work together. In other words how can I intergrate RMI/IIOP into a Jersey application.
I was thinking of something like this:
#Path("/items")
public class ItemsResource {
#Context
UriInfo uriInfo;
#Context
Request request;
Stuber s = new Stuber();
#GET
public Response get() throws RemoteException {
}
Were I have an external class in the Jersey Project that will work as a client to connect with the RMI/IIOP
public class Stuber {
Context ic;
iRemoteLogic logic;
public Stuber() {
super();
Object objref;
try {
ic = new InitialContext();
objref = ic.lookup("LogicService");
System.out.println("Client: Obtained a ref. to Hello server.");
logic = (iRemoteLogic) PortableRemoteObject.narrow(
objref, iRemoteLogic.class);
What should I add to the Stuber class to be able to work as an RMI/IIOP client?
Thanks :)
NOTE: I followed this tutorial for the RMI/IIOP
You would need to provide somewhere an implementation of iRemoteService that is exported via RMI/IIOP (i.e. PortableRemoteObject), and register it via JNDI as LogicService. I doubt the latter is going to work: surely you will need to provide a protocol and host to JNDI.

Categories

Resources