SAML generate metadata from entityId - web-services

I am trying to generate SAML metadata for Service provider,I have created simple servlet and trying to generate metadata in a methoad doGet()
this is the code I have tried below
import java.io.IOException;
import java.io.PrintWriter;
import java.net.HttpRetryException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.namespace.QName;
import org.opensaml.Configuration;
import org.opensaml.saml2.metadata.EntityDescriptor;
import org.opensaml.xml.XMLObjectBuilderFactory;
public class MetadataProviderServlet extends HttpServlet{
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException{
PrintWriter writer = response.getWriter();
response.setContentType("text/html");
writer.println("Hello world");
String entityId = "www.sampleEntityId.com";
XMLObjectBuilderFactory builderFactory = Configuration.getBuilderFactory();
EntityDescriptor descriptor = (EntityDescriptor)(builderFactory.getBuilder(EntityDescriptor.DEFAULT_ELEMENT_NAME).buildObject(EntityDescriptor.DEFAULT_ELEMENT_NAME));
descriptor.setEntityID(entityId);
}
}
But when I run the web application using one application server I got the NUllpointer Exception in particularly creation the object Entity Descriptor.Can Anyone please figure out what went wrong?

You need to initialise the OpenSAML library first. OpenSAML is initialised using the bootstrap method.
DefaultBootstrap.bootstrap();
I write about this on my blog here

Related

Problem is deploying REST Web service with Tomee

I try to deploy a REST web service on my tomee.
i use apache-tomee-plus-8.0.6 with JDK 15
I create a web application called test
I create a class like this:
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
#ApplicationPath("/test")
public class WebServiceApplication extends Application {
#Override
public Set<Class<?>> getClasses() {
return new HashSet<Class<?>>(Arrays.asList(QueueContentWebService.class));
}
}
And another like this:
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
#Path("/queuecontent")
public class QueueContentWebService {
#POST
public Response getContent() {
return Response.status(200).entity("getContent is called").build();
}
}
Then i try this URL, with a browser and with postman in POST:
http://localhost:8080/test/test/queuecontent/
I only get 404 response... It seems that my web service is not deployed. Anybody can help me by saying what is missing?

S3 Access in a JMeter script

I'm trying to get a file from an S3 bucket as part of the Setup Thread Group for a JMeter test, and I have the following Groovy script in a JSR223 Sampler:
import java.io.IOException
import java.io.InputStream
import java.util.Properties
import com.amazonaws.services.s3.AmazonS3
import com.amazonaws.services.s3.AmazonS3ClientBuilder
import com.amazonaws.services.s3.model.GetObjectRequest
import com.amazonaws.services.s3.model.S3Object
import com.amazonaws.auth.BasicAWSCredentials
import com.amazonaws.auth.AWSStaticCredentialsProvider
import com.amazonaws.services.s3.S3CredentialsProviderChain
def awsAccessKey = "my_key"
def awsSecretAccessKey = "my_secret_key"
System.setProperty("aws.accessKeyId", awsAccessKey)
System.setProperty("aws.secretKey", awsSecretAccessKey)
def awsCreds = new BasicAWSCredentials(awsAccessKey, awsSecretAccessKey)
def s3Client = AmazonS3ClientBuilder.standard()
.withRegion("us-west-2")
.build()
def s3Object = s3Client.getObject(new GetObjectRequest("bucket-name", "filename"))
def is = s3Object.getObjectContent()
The code throws the following, both when I use .withCredentials(awsCreds) and when I set the system properties:
java.lang.NoClassDefFoundError: Could not initialize class com.amazonaws.services.s3.S3CredentialsProviderChain
I've also converted the same code to the following Beanshell Sampler:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.auth.BasicAWSCredentials;
String awsAccessKey = "my_key";
String awsSecretAccessKey = "my_secret_key";
System.setProperty("aws.accessKeyId", awsAccessKey);
System.setProperty("aws.secretKey", awsSecretAccessKey);
BasicAWSCredentials awsCreds = new BasicAWSCredentials(awsAccessKey, awsSecretAccessKey);
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion("us-west-2").build();
S3Object s3Object = s3Client.getObject(new GetObjectRequest("bucket-name", "filename"));
InputStream is = s3Object.getObjectContent();
However, this results in the following:
org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval
Sourced file: inline evaluation of: ``import java.io.IOException; import java.io.InputStream; import java.util.Propert . . . '' : Typed variable declaration : Method Invocation AmazonS3ClientBuilder.standard
When I put this same code into a test Java class, it runs with no issues. I have aws-java-sdk-1.11.160.jar and aws-java-sdk-core-1.11.160.jar in my jmeter/lib, so I don't believe it's a missing dependency. Am I missing something else?
Try to set credentials explicitly, as you build your client instance.
Like,
...
BasicAWSCredentials awsCreds = new BasicAWSCredentials(awsAccessKey, awsSecretAccessKey);
AWSStaticCredentialsProvider awsCredsProvider = new AWSStaticCredentialsProvider(awsCreds);
AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withRegion("us-west-2").withCredentials(awsCredsProvider).build();
For the groovy way, for class class com.amazonaws.services.s3.S3CredentialsProviderChain, you need aws-java-sdk-s3-1.11.161.jar in your path.
https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3
The beanshell error message isn't helpful, it is just saying there is an error in your code somewhere but doesn't really say where. I guess you could turn on additional debugging to find out, but I'd stick with groovy.
In aws sdk java it stated:
To use the SDK, add the full path to the lib and third-party directories to the dependencies in your build file, and add them to your java CLASSPATH to run your code.
You need to include the three specified Jackson libraries as well. That will correct the issue.

Multiple versions of REST web service - Implementation error

I am trying to create a v2 of a web service. For this I created a new package com.package.v2 where the remote interface has been declared. If I keep the class name of v2 same as v1, then I get an error during the deployment to glassfish server.
SEVERE|glassfish3.1.2|com.sun.jersey.spi.inject.Errors|_ThreadID=61;_ThreadName=Thread-2;|The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: The class com.package.v2.TestRESTServiceBA is an interface and cannot be instantiated.|#]
If I rename the v2 class to a different name then everything works fine. Can you please let me know how I can keep both the version with the same name?
More details:
INFO|glassfish3.1.2|com.sun.jersey.api.core.ScanningResourceConfig|_ThreadID=61;_ThreadName=Thread-2;|Root resource classes found:
interface com.package.v2.TestRESTServiceBA
interface com.package.v1.TestRESTServiceBA
Adding Code:
Version 1 Interface
package com.package.v1;
import java.util.Map;
import javax.ejb.Remote;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import com.package.common.dto.ResponseDTO;
#Remote
#Path("/v1/test")
public interface TestRESTServiceBA {
#GET
#Path("/headers")
#Produces("application/json")
ResponseDTO<Map<String, String>> getAllHeaders();
}
Version 1 Implementation
package com.package.v1;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.Stateless;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.package.common.dto.ResponseDTO;
import com.package.v1.TestRESTServiceBA;
#Stateless
public class TestRESTServiceBABean implements TestRESTServiceBA {
private static final Log LOG = LogFactory.getLog(TestRESTServiceBABean.class);
#Context
private HttpServletRequest request;
public TestRESTServiceBABean() {
}
#Override
public ResponseDTO<Map<String, String>> getAllHeaders() {
LOG.info("GET:v1/test/headers requested!");
final ResponseDTO<Map<String, String>> response = new ResponseDTO<>();
return response;
}
}
Version 2 interface
package com.package.v2;
import java.util.Map;
import javax.ejb.Remote;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import com.package.common.dto.ResponseDTO;
#Remote
#Path("/v2/test")
public interface TestRESTServiceBA2 {
#GET
#Path("/headers")
#Produces("application/json")
ResponseDTO<Map<String, String>> getAllHeaders();
}
Version 2 Implementation
package com.package.v2;
import java.util.HashMap;
import java.util.Map;
import javax.ejb.Stateless;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.package.common.dto.ResponseDTO;
import com.package.common.model.DealerSpeedUser;
import com.package.v2.TestRESTServiceBA2;
#Stateless
public class TestRESTServiceBABean implements TestRESTServiceBA {
private static final Log LOG = LogFactory.getLog(TestRESTServiceBABean2.class);
#Context
private HttpServletRequest request;
public TestRESTServiceBABean2() {
}
#Override
public ResponseDTO<Map<String, String>> getAllHeaders() {
LOG.info("GET:v2/test/headers requested!");
final ResponseDTO<Map<String, String>> response = new ResponseDTO<>();
return response;
}
}
The error is telling you it can't create an instance of an interface because you've put the #Path #GET, #Produces, etc. annotations on the interface classes. Move those annotations to the TestRESTServiceBABean classes instead.

#Resource private WebServiceContext context is giving null value

I am trying to take ServletContext in my web service ,but WebServiceContext is always NULL.Can anyone tell what am I doing wrong, I am using spring framework and tomcat.
package com.xyz.webser;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletContext;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.handler.MessageContext;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
#WebService(serviceName = "myWebService", endpointInterface = "com.xyz.webser.MyWebService")
public class MyWebServiceImpl implements MyWebService {
#Resource
private WebServiceContext context;
public void setContext(WebServiceContext context) {
this.context = context;
}
I have created a new java file in service package used "implements ServletContextAware"
called that method from web service.

Hector + Cassandra: Get Column Family List

I am trying to get all the column families in the current keyspace I am using because I want to get rid of the error:
InvalidRequestException(why:[column family] already exists in keyspace)
My logic is to get all the Column Families in the current key space & check whether or not a particular column family appears in the returned list. So, I try:
KeyspaceDefinition keyspaceDef = HFactory.createKeyspaceDefinition("test");
...
List<ColumnFamilyDefinition> lsCf = keyspaceDef.getCfDefs();
There seems to be a problem with creating the
List<ColumnFamilyDefinition> lsCf = keyspaceDef.getCfDefs();
I did a System.out.println(keyspaceDef.getCfDefs()) and it returned
[]
an empty list - which is what I expected. What I cannot understand is why List<ColumnFamilyDefinition> lsCf = keyspaceDef.getCfDefs(); is incorrect. Eclipse disagrees with the "List" portion of this line. Other than that, it appears that he code is right. Can some one please help me understand why this line is wrong or whether my approach is off?
Here's the full code snippet:
package org.cassandra.examples;
import me.prettyprint.cassandra.model.BasicColumnFamilyDefinition;
import me.prettyprint.cassandra.service.CassandraHostConfigurator;
import me.prettyprint.cassandra.service.ThriftCfDef;
import me.prettyprint.cassandra.service.ThriftCluster;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.exceptions.HectorException;
import me.prettyprint.hector.api.factory.HFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.*;
import me.prettyprint.cassandra.model.BasicColumnDefinition;
import me.prettyprint.cassandra.model.BasicColumnFamilyDefinition;
import me.prettyprint.cassandra.model.thrift.ThriftConverter;
import org.apache.cassandra.db.ColumnFamily;
import org.apache.cassandra.thrift.Cassandra;
import me.prettyprint.cassandra.service.ThriftKsDef;
import me.prettyprint.hector.api.*;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.beans.HSuperColumn;
import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
import me.prettyprint.hector.api.ddl.ColumnIndexType;
import me.prettyprint.hector.api.ddl.ComparatorType;
import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;
import me.prettyprint.cassandra.service.template.ColumnFamilyTemplate;
import me.prettyprint.cassandra.service.template.ColumnFamilyUpdater;
import me.prettyprint.cassandra.service.template.ThriftColumnFamilyTemplate;
import me.prettyprint.hector.api.mutation.Mutator;
import me.prettyprint.hector.api.query.ColumnQuery;
import me.prettyprint.hector.api.query.QueryResult;
import me.prettyprint.hector.api.query.SuperColumnQuery;
public class HectorTest {
private static String keyspaceName = "test3";
private static KeyspaceDefinition newKeyspaceDef;
private static Cluster cluster;
private static Keyspace ksp;
public static void main(String[] args) {
cluster = HFactory.getOrCreateCluster("test cluster", "xxx.xxx.x.xx:9160");
newKeyspaceDef = HFactory.createKeyspaceDefinition(keyspaceName);
ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition("MyKeyspace",
"ColumnFamilyName",
ComparatorType.BYTESTYPE);
List<ColumnFamilyDefinition> lCf = newKeyspaceDef.getCfDefs(); //= new ArrayList<ColumnFamilyDefinition>();
if((cluster.describeKeyspace(keyspaceName)) == null){
createSchema();
}
ksp = HFactory.createKeyspace(keyspaceName, cluster);
//Articles art = new Articles(cluster, newKeyspaceDef);
//cluster.dropColumnFamily(keyspaceName, "Articles");
}
public static void createSchema(){
cluster.addKeyspace(newKeyspaceDef,true);
}
}
Error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
List cannot be resolved to a type
at org.cassandra.examples.HectorTest.main(HectorTest.java:55)
Add
import java.util.List;
to you imports. In eclipse, CTRL-SHIFT-O will organize your imports for you, and add anything that is missing.