listing emails from gmail with spring boot - java

I want to retrieve all my emails from Gmail with spring
I found that spring integration provide a class named ImapMailReceiver
but this doesn't work
the documentation says that I have to add java mail api implementation SO I added this dependency angus-mail
try {
ImapMailReceiver imapMailReceiver = new ImapMailReceiver(s);
imapMailReceiver.receive();
} catch (MessagingException e) {
throw new RuntimeException(e);
}
s="imap://username#gmail.com:pass#imap.gmail.com:993/INBOX"
Is there anyone who has a suggestion for how to proceed ?
ERROR:
Factory method 'mailReceiver' threw exception with message: jakarta.mail.MessagingException: Connection dropped by server?

The Spring Integration Mail module is fully based on Java Mail API. How to configure a Java Mail client is out of Spring Integration scope. So, make yourself familiar with GMail requirements for Java Mail API first. The go through ImapMailReceiver Javadocs to see if you need to provide anything else, not just an URL:
https://github.com/google/gmail-oauth2-tools/tree/master/java
https://www.tzhwang.com/2022/01/28/using-javamail-to-search-gmail-with-imap/

Related

How to Configure using STSAssumeRoleSessionCredentialsProvider in spring-cloud-stream-binder-kinesis

I am using spring-cloud-stream-binder-kinesis, version: 2.0.2.RELEASE.
I was able to successfully use binder and access it locally using the default ContextCredentialsAutoConfiguration mentioned in the KinesisBinderConfiguration.
Now I know this set-up wont work for me because,
The Kinesis data stream is in AWS account 1
The Service is running in AWS account 2
(I have already done the setup of assumed Role so that Account 2 can access streams in account 1 using the assumed role)
However I am not sure how can I override the credentials in binder to use STSAssumeRoleSessionCredentialsProvider
Can someone help please?
The KinesisBinderConfiguration is fully based on the auto-configuration from the Spring Cloud AWS, which provides for us a ContextCredentialsAutoConfiguration and expose an AWSCredentialsProvider bean under the credentialsProvider name if not present yet.
So, probably you just need to have your STSAssumeRoleSessionCredentialsProvider as a bean in your configuration class and give it that credentialsProvider bean name.

Is it possible to run a gRPC server in a Java application using WildFly?

I'm trying to run a gRPC server in our WildFly application to connect to from a client Java microservice using Quarkus.
I was able to set up a running server on port 9002 for gRPC where the client microservice can connect to.
When doing a call to the running gRPC service, I can see the rpc function is executed inside the WildFly application. I'm running into 2 issues however.
For the Server and Service setup, I used this code as an example.
Issues
Dependency injection does not work in this service. The bean is created in WildFly but whatever object I #Inject into the gRPC Service, it always has a null value.
The server executes the code and replies with an appropriate gRPC message but this message never arrives at client side. No error is displayed whatsoever.
Does anyone have experience with these issues or knows if it is even possible to run a gRPC server within a WildFly application?
What I tried:
I tried to find an existing gRPC integration with WildFly but this is currently in the works as I could derive from this open pull request.
From this mailing list it seems that someone made a Proof Of Concept and made changes to the WildFly code to make it work, however this is not the solution that we are looking for since we'd prefer not to change the WildFly code.
Relevant code
The client code hangs on the blockingStub.updateBalance(request) call because no reply is ever received:
BalanceUpdateGrpc.BalanceUpdateBlockingStub blockingStub = BalanceUpdateGrpc.newBlockingStub(channel);
var request = BalanceUpdateRequest.newBuilder()
.setContractNumber("111")
.setBalance((float) 100.2)
.setDate(BalanceUpdateRequest.Date
.newBuilder()
.setDay(3)
.setMonth(11)
.setYear(2020)
.build())
.build();
var reply = blockingStub.updateBalance(request);
The service code:
#Singleton
public class BalanceUpdateService extends BalanceUpdateGrpc.BalanceUpdateImplBase {
#Inject
private DossierDAO dossierDAO;
#Override
public void updateBalance(BalanceUpdateRequest balanceUpdateRequest, StreamObserver<BalanceUpdateReply> responseObserver) {
//Just reply with true
//dossierDAO is always null when used in this code block
responseObserver.onNext(BalanceUpdateReply.newBuilder().setSuccess(true).build());
}
}
I don't have experience with WildFly but it seems the classic issue related to the application context. It happens when you get an object with NEW instead of through the IOC. When you do that, the injections don't work.
Do know Spring? Spring has the same concept. Maybe you can transpose this Spring code to WildFly
public static void main(String[] args)throws IOException, InterruptedException {
SpringApplication springApplication = new SpringApplication(App.class, GrpcConfig.class);
//Getting application context with all objects created with annotations
ConfigurableApplicationContext context = springApplication.run(args);
// Getting object created by Spring "Wildfly" from outside the context
GrpcServer grpcServer = context.getBean(GrpcServer.class);
grpcServer.start();
}
Here is an example of gRPC and Spring which can help you. https://github.com/apssouza22/modern-api-management/blob/master/services/shelf/src/main/java/com/apssouza/shelf/App.java
For this issue, you just need to know better how your framework works
I needed to add:
responseObserver.onCompleted()
to the service to get the response sent back to the client.
The null injection is still an issue however.

How can I get the Discovery Client working when using spring-cloud together with netflix Eureka?

I'm trying to make a basic project using spring cloud with the netflix addons such as Hystrix, Eureka and Ribbon to learn how this works. The project I'm trying to make is a simple message server that will keep messages. And a message-client that will just ask the server for a message, and I want to use the auto discovery client for this, or the RestTemplate discovery. But I can't get either to work.
I have the following structure:
message-client (eureka client)
message-server (eureka client)
configuration-service (config server)
discovery-service (eureka server)
What I currently do is I start up the configuration-service, and expose the application.yml details to all of these "apps/clients" when they are connecting by the following structure:
config-service\src\main\resources\config\appname.yml
app\src\main\resources\bootstrap.yml (contains the appname and url to cloud config)
This is working just fine, and my apps start up on the port they receive from the config server, as well as they all connect to my eureka server, and all of them are visible there. As well as the Hystrix failover is also working, not that it is related to this but it tells me that it can't be completely wrong then.
But here comes my confusion...
When using the #Autowired annotation in my service class (#Service annotated) inside my client module, I get a discoveryClient object, but I am unable to find any other services using that one.
Message Client - boot class:
#EnableAutoConfiguration
#EnableHystrix
#EnableEurekaClient
#ComponentScan("cloud.rest.resources, spring.cloud.client")
public class ClientBoot {
public static void main(String[] args) {
SpringApplication.run(ClientBoot.class, args);
}
}
Message Client - REST Resource:
#RestController
public class MessageResource {
#Autowired
private MessageClient messageClient;
#RequestMapping(value = "/message/{client}", method = RequestMethod.GET)
public Message getMessage(#PathVariable String client) {
return messageClient.getMessage(client);
}
}
Message Client - MessageClient:
#Service
public class RestMessageClient implements MessageClient {
#Autowired
private DiscoveryClient discoveryClient;
#Autowired
private RestTemplate restTemplate;
#Override
public Message getMessage(String client) {
return restTemplate.getForObject(String.format("http://message-server/message/%s", client), Message.class);
}
}
My message server boot class that is holding the messages has the same annotations as my client one.
And as I said, my service class are unable to find anything..
Which leads me to all of my questions:
What is required to actually use ribbon load balancer?
Do I have to use ribbon to be able to use the "auto discovery", I thought not but now I'm just confused.
From what I've understood, when using EnableEurekaClient I should not need to use the EnableDiscoveryClient as well?
Can I change the yml files on my config-server for the clients in runtime and just have to reboot the client?
How much configuration is really meant to be shared by the config-server, because currently all of my clients just contain a super basic bootstrap.yml file.
Does anyone have a good link to where I can read more about all the properties that is being set in my yml files? Both a documentation of what the properties that exists actually do as well as some documentation on how I can use them in combination with spring cloud?
Do I need specific properties to enable my apps/clients to find other apps/clients?
Edited information
Thank you for your quick and excellent reply, I've gone through this over and over today and I finally got my application working..
The problem (I can't understand why and was hoping you could help me understand that) is that my discovery service contains yml files for each of my other clients where I specify things like port and eureka information.. What I specified here as well was:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka
So, when I set this value it seems to override something that makes my service discovery not working.. Even tho I can see all my applications in the eureka server, they were unable to find each other when I had this value set.
I set this value by having a message-server.yml file in my configuration service that is sent out to my message-server application after bootstrap..
So then I have two new questions.
How do I override this eureka server property?
Why does my discovery client stop working when I set this value, what is it that it actually does?
What is required to actually use ribbon load balancer?
The ribbon-loadbalancer must be on the classpath (e.g. via "spring-cloud-starter-ribbon"). Then you can inject one as a LoadBalancerClient or you can inject a RestTemplate (it will be load-balancer aware if you have a LoadBalancerClient).
Do I have to use ribbon to be able to use the "auto discovery", I thought not but now I'm just confused.
What is "auto discovery"? You don't need to use Ribbon to use the DiscoveryClient (Ribbon is a load balancer, not a service registry).
From what I've understood, when using EnableEurekaClient I should not need to use the EnableDiscoveryClient as well?
Correct. #EnableEurekaClient is annotated with #EnableDiscoveryClient so it is only there to express a preference.
Can I change the yml files on my config-server for the clients in runtime and just have to reboot the client?
Yes. Or you can use the /refresh or /restart endpoints (a full reboot is probably best in production, at least periodically).
How much configuration is really meant to be shared by the config-server, because currently all of my clients just contain a super basic bootstrap.yml file.
As much as you want. How long is a piece of string? If I were you I would try and keep the central config to a minimum (only the things that change between environments, or at runtime).
Does anyone have a good link to where I can read more about all the properties that is being set in my yml files? Both a documentation of what the properties that exists actually do as well as some documentation on how I can use them in combination with spring cloud?
Spring Boot and Spring Cloud have autogenerated metadata for externalized properties. The new generation of IDEs understands them (so get STS 3.6.4 or IDEA 14.1), and they are listed in the user guide (for Spring Boot at least) under http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#common-application-properties
Do I need specific properties to enable my apps/clients to find other apps/clients?
You need to be able to locate your service registry (Eureka in this case). If you are using Eureka and your clients have registered then that is enough.

How to call a web security with message security and client certificate authentication?

I need to call a web service with a java client.
This service authenticates clients through certificates at the message level (Ws-Security, not SSL).
It should be possible since, I can generate web services with JAX-WS with mutual certificate security in this dialog.
But I don't manage to create a client. Does anyone has an idea ?
I did not tried it myself, but from http://download.oracle.com/docs/cd/E17802_01/webservices/webservices/docs/2.0/tutorial/doc/ :
Configuring Message Security Using XWSS
The Application Server contains all of the JAR files necessary to use XWS-Security for securing JAX-WS applications, however, in order to view the sample applications, you must download and install the standalone Java WSDP bundle. You can download the Java WSDP from http://java.sun.com/webservices/downloads/webservicespack.html.
To add message security to an existing JAX-WS application using XWSS, follow these steps on the client side:
Create a client security configuration. The client security configuration file specifies the order and type of message security operations that will be used for the client application. For example, a simple security configuration to perform a digital signature operation looks like this:
<xwss:Sign id="s" includeTimestamp="true">
<xwss:X509Token encodingType="http://docs.oasis-
open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"
valueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-
x509-token-profile-1.0#X509SubjectKeyIdentifier"
certificateAlias="xws-security-client"
keyReferenceType="Identifier"/>
</xwss:Sign>
</xwss:SecurityConfiguration>
</xwss:Service>
<xwss:SecurityEnvironmentHandler>
simple.client.SecurityEnvironmentHandler
</xwss:SecurityEnvironmentHandler>
For more information on writing and understanding security configurations and setting up SecurityEnvironmentHandlers, please see the Java Web Services Developer Pack 1.6 Tutorial at http://java.sun.com/webservices/docs/1.6/tutorial/doc/index.html.
In your client code, create an XWSSecurityConfiguration object initialized with the security configuration generated. Here is an example of the code that you would use in your client file. For an example of a complete file that uses this code, look at the example client in the \jaxws2.0\simple-doclit\src\simple\client\ directory.
FileInputStream f = new FileInputStream("./etc/client_security_config.xml");
XWSSecurityConfiguration config = SecurityConfigurationFactory.newXWSSecurityConfiguration(f);
Set security configuration information on the RequestContext by using the XWSSecurityConfiguration.MESSAGE_SECURITY_CONFIGURATION property. For an example of a complete file that uses this code, look at the example client in the \jaxws2.0\simple-doclit\src\simple\client\ directory.
// put the security config info
((BindingProvider)stub).getRequestContext().put(
XWSSecurityConfiguration.MESSAGE_SECURITY_CONFIGURATION,
config);
Invoke the method on the stub as you would if you were writing the client without regard to adding XWS-Security. The example for the application from the \jaxws2.0\simple-doclit\src\simple\client\ directory is as shown below:
Holder<String> hold = new Holder("Hello !");
stub.ping(ticket, hold);

No available client connector supports the required protocol: 'HTTP'

Greeting. I am playing with Restlet framework, when I am running following code getting and exception Internal Connector Error (1002) - No available client connector supports the required protocol: 'HTTP'.
ClientResource cr = new ClientResource(
"http://127.0.0.1:8888/user/123");
UserResource resource = cr.wrap(UserResource.class);
User user = new User();
user.setName("xxx");
user.setPassword("xxx");
UserValidation userValidation = resource.retrieve(user);
if (userValidation != null) {
System.out.println("Welcome, User");
} else {
System.out.println("Not a vliad user");
}
Is there anybody here tried the Retlet before? Can guide me to proper direction?
Or can redirect to helpfull tutorial?
Restlet depends on "connectors" to implement clients and servers. They're kind of like plugins, in that they're modular, and you can easily choose which to use. At runtime, Restlet checks the classpath to see which connectors are available, and loads them. Each connector is packaged in 1 JAR file for the connector itself plus N files for dependencies. So you can make a connector available to the framework by simply adding the relevant JARs to the classpath.
You must be using Restlet 1.0 or 1.1, because 2.0 includes simple built-in HTTP client and server connectors. I suggest you upgrade to 2.0; it's a lot easier to develop with because a single JAR contains the framework, engine, and the built-in connectors; with 1.1 you can sometimes need 6-7 JARs just to test a simple app.

Categories