java.net.SocketTimeoutException: connect timed out + google directory , admin sdk API - credentials

I am getting the berlow error while using the service account.
The same code works well in local and iam able to create the credentials using service account.
....Error Snapshot :
aused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:618)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:378)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:473)
at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:270)
at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:327)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:931)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:1090)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:250)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:77)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:965)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:283)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:269)

Try the following ...
Set the proxy in the linux box.
While creating storage - new NetHttpTransport() provide your own way to create the socket and add the proxy in the transport.
Example code ...
public HttpClient myHttpClient() throws Exception {
SchemeRegistry schemeRegistry = new SchemeRegistry();
//SetRegisrty for both HTTP and HTTPS - Check google for this.
schemeRegistry.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), YOUR_PROXY_PORT));
schemeRegistry.register(new Scheme("https", SSLSocketFactory
.getSocketFactory(), 443));
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, 30 * 1000); // SET the timeout
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
ClientConnectionManager connManager = new ThreadSafeClientConnManager(
params, schemeRegistry);
DefaultHttpClient httpClient = new DefaultHttpClient(connManager,
params);
try {
int proxyPort = YOUR_PROXY_PORT;
String proxyHost = "YOUR_PROXT_HOST_NAME";
if (proxyPort > 0 && proxyHost != null && proxyHost.length() > 0) {
System.setProperty("https.proxyHost", proxyHost);
System.setProperty("https.proxyPort", proxyPort + "");
System.setProperty("http.proxyHost", proxyHost);
System.setProperty("http.proxyPort", proxyPort + "");
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
httpClient.getParams().setParameter(
ConnRoutePNames.DEFAULT_PROXY, proxy);
}
} catch (NullPointerException e) {
System.out.println("Proxy error here");
}
return httpClient;
}
public static HttpTransport myNetHttpTransport()
throws Exception {
return new ApacheHttpTransport(myHttpClient());
}
use .setTransport(myNetHttpTransport()) instead of NetHttpTransport() .
we spent long time on this. but as of now this seems to be working. Please let me know if any help in this...

Related

Facing Caused by: java.net.UnknownHostException: login.microsoftonline.com

I am calling microsoft Azureurl with restTemplate in springboot application
Url:-https://login.microsoftonline.com/{{tenantId}}/oauth2/v2.0/token
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.add("grant_type","client_credentials");
map.add("client_id","xxxxxxxxxxxxxxxxxxxxxxxxxx");
map.add("client_secret","xxxxxxxxxxxxxxxxxx");
map.add("scope","https://graph.microsoft.com/.default");
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(map, headers);
ResponseEntity<String> response =
restTemplate.exchange("https://login.microsoftonline.com/xxxxxxxxxx-xxxxxx-c666dbeda42c\n/oauth2/v2.0/token",
HttpMethod.POST,
entity,
String.class);
if (response.getStatusCode() == HttpStatus.OK) {
System.out.println("Request Successful");
} else {
System.out.println("Request Failed");
}
While running the above sample call to connect with Azure, we are getting the Exception as : Caused by: java.net.UnknownHostException: login.microsoftonline.com
Later tried at home, where there is no Proxy to connect to the internet, and then am able to successfully get output without this "UnknownHostException" error.
So how to resolve the issue.
Your url seems to be not specification https://login.microsoftonline.com/xxxxxxxxxx-xxxxxx-c666dbeda42c\n/oauth2/v2.0/token, try to change it to: https://login.microsoftonline.com/xxxxxxxxxx-xxxxxx-c666dbeda42c/oauth2/v2.0/token . Remove \n.
I have the same error for a missing proxy configuration. (I was googling for solutions for a specific case)
"Host not found" has nothing to do with the url parameters, but as said just before, having "\n" in an url is never a good idea.

Adding client tags to presto jdbc connection

I am using jdbc to connect to presto server.
From the documentation, I am able to connect to the server and run queries.
https://prestodb.io/docs/current/installation/jdbc.html
I am facing issues while sending ClientTags (X-Presto-Client-Tags) in the connection/statement.
Here's the function to build a connection and run a query.
public void test() {
java.util.Properties info = new java.util.Properties();
if (PRESTO_USER != null) {
info.put("user", PRESTO_USER);
}
if (PRESTO_PASSWORD != null) {
info.put("password", PRESTO_PASSWORD);
}
info.put("ClientTags", "my,tag");
try (Connection connection = DriverManager.getConnection(PRESTO_URL, info);
Statement statement = connection.createStatement()) {
testBody(connection, statement);
} catch (Exception ex) {
ex.printStackTrace();
fail("Exception occured");
}
}
However, it fails with
java.sql.SQLException: Unrecognized connection property 'ClientTags'
at com.facebook.presto.jdbc.PrestoDriverUri.validateConnectionProperties(PrestoDriverUri.java:316)
For pyhive, I was able to override the session and pass client tags
https://github.com/dropbox/PyHive/issues/283. Can this be done for jdbc driver too?
It's currently not possible to set ClientTags on the connection URL.
Please create a feature request # https://github.com/trinodb/trino/issues/
Currently, the only way is to use Connection method:
Connection connection = DriverManager.getConnection("....");
connection.unwrap(PrestoConnection.class)
.setClientInfo("ClientTags", "one,two,three");

Unable to connect to Azure Cosmos Db Account using Microsoft.EntityFrameworkCore.Cosmos - Response status code

The CosmosDb provider is sending this message:
“Response status code does not indicate success: 503 Substatus: 0 Reason: (The request failed because the client was unable to establish connections to 3 endpoints across 1 regions. Please check for client resource starvation issues and verify connectivity between client and server.”
In my tests, it works (.net core 3.1):
Task.Run(async () =>
{
var endpoint = “test”;
var masterKey = “test”;
using (var client = new DocumentClient(new Uri(endpoint), masterKey))
{
//Insert new Document
Console.WriteLine("\r\n>>>>>>>>>>>>>>>> Creating Document <<<<<<<<<<<<<<<<<<<");
dynamic candidato = new
{
Id = 1,
Nome = "Test"
};
var document1 = await client.CreateDocumentAsync(
UriFactory.CreateDocumentCollectionUri("Test", "Test"),
candidato);
Console.ReadKey();
}
}).Wait();
It does not:
Task.Run(async () =>
{
using (var context = new StudentsDbContext())
{
context.Add(new FamilyContainer(2, "Test"));
await context.SaveChangesAsync();
}
}).Wait();
public class FamilyContainer
{
public int Id { get; set; }
public string Nome { get; set; }
public FamilyContainer(int id, string nome)
{
Id = id;
Nome = nome;
}
}
public class StudentsDbContext : DbContext
{
public DbSet<FamilyContainer> FamilyContainer { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseCosmos(
"test",
"test",
"FamilyDatabase",
options =>
{ }
);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<FamilyContainer>(x =>
{
x.ToContainer("FamilyContainer");
});
}
}
Packages
Can anyone help me? Thanks
fail: Microsoft.EntityFrameworkCore.Update[10000]
An exception occurred in the database while saving changes for context type '...'.
Microsoft.EntityFrameworkCore.Storage.RetryLimitExceededException: Maximum number of retries (6) exceeded while executing database operations with 'CosmosExecutionStrategy'. See inner exception for the most recent failure.
---> Microsoft.Azure.Cosmos.CosmosException : Response status code does not indicate success: 503 Substatus: 0 Reason: (Microsoft.Azure.Documents.ServiceUnavailableException: Service is currently unavailable.ActivityId: 07fbf539-0d44-4e5a-89d0-cd46838ee605, {"RequestStartTimeUtc":"2020-02-21T16:34:09.1834993Z","RequestEndTimeUtc":"2020-02-21T16:34:41.3484203Z","RequestLatency":"00:00:32.1649210","IsCpuOverloaded":false,"NumberRegionsAttempted":1,"ResponseStatisticsList":[{"ResponseTime":"2020-02-21T16:34:11.5964152Z","ResourceType":2,"OperationType":0,"StoreResult":"StorePhysicalAddress: rntbd:.../, LSN: -1, GlobalCommittedLsn: -1, PartitionKeyRangeId: , IsValid: True, StatusCode: 410, SubStatusCode: 0, RequestCharge: 0, ItemLSN: -1, SessionToken: , UsingLocalLSN: False, TransportException: A client transport error occurred: Failed to connect to the remote endpoint. (Time: 2020-02-21T16:34:11.5298608Z, activity ID: 07fbf539-0d44-4e5a-89d0-cd46838ee605, error code: ConnectFailed [0x0005], base error: socket error ConnectionRefused [0x0000274D]...
--- End of inner exception stack trace ---
I was facing same issue.
What worked for me is changing ConnectionMode to ConnectionMode.Gateway while initializing CosmosClient like :
var options = new CosmosClientOptions() { ConnectionMode = ConnectionMode.Gateway };
var client = new CosmosClient(endpoint, key, options);
For more details on refer :
https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.cosmos.cosmosclientoptions?view=azure-dotnet
https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.cosmos.connectionmode?view=azure-dotnet
TransportException: A client transport error occurred: Failed to connect to the remote endpoint. (Time: 2020-02-21T16:34:11.5298608Z, activity ID: 07fbf539-0d44-4e5a-89d0-cd46838ee605, error code: ConnectFailed [0x0005], base error: socket error ConnectionRefused
This means that the Connection was refused.
Either your Cosmos DB account has Firewall/VPN enabled and the application is not able to establish a connection due not not being in a whitelisted IP/Network : Try checking your account configuration.
The environment you are executing the code is restricting connections (some corporate Firewall or network might be blocking port ranges): Try running the app in a different network, or use GatewayMode. If that works, then this is related to the network.
The machine might be running low on sockets or high on CPU.
My RCA for this is: Cosmos Partitions where served by individual processes on CosmosDB, each partition serving process has it's own TCP port. When client connects to 443 (Using TCP Direct Mode), CosmosDB Proxy sends partition ports back to client so that client can talk to server-partitions in parallel. Partition ports are random (11000 upwards afaik). Normal company firewall would allow outbound 443 (connection to cosmos works) but blocks the outbound random ports. So at the end, access fails.
Workarounds:
Open firewall
Use Gateway Mode. This uses https/443 only by forwarding internally instead of redirecting to other ports.
It is because Entity framework has a default connection mode of Direct. It worked for me after overriding it to Gateway.
{
optionsBuilder.UseCosmos(
"test",
"test",
"FamilyDatabase",
options =>
{ options.ConnectionMode(ConnectionMode.Gateway); }
);
}
I just want to add this because it wasted a lot of my time. The following code would instantly die with an error message that led me to this S.O. post:
var container = _client.Client.GetContainer(_databaseName, containername);
var result = await container.CreateItemAsync(dataitem, pk);
I disbelieved the error message because everything else has worked, upsert, read, etc. After messing with it for a while, I noticed the documentation shows a template type for CreateItemAsync.
var container = _client.Client.GetContainer(_databaseName, containername);
var result = await container.CreateItemAsync<T>(dataitem, pk);
Changing the code to that fixed it (inside of a templated function).
I wanted to add: if I had been catching exceptions, I would have gotten to the meat of the problem much sooner. The library I am working with is not meant to catch exceptions, they are handled by the layer above it.

Windows azure - Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host

We are getting the below exception while reading data using JsonTextReader
Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
JsonTextReader jsonReader - parameter
while (hasRecords(jsonReader, JsonToken.StartObject, null, null)) //Row
{
...
//it's ok to read this all into memory - it's just one row's worth of data
JArray values = (JArray)JToken.ReadFrom(jsonReader);
Also, including the code for HttpPost implementation for better clarity
HttpClientHandler handler = new HttpClientHandler() { Credentials = taskProfileInfo.Credential };
HttpClient httpClient = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(taskProfileInfo.CommandTimeout) };
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response;
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url);
request.Content = new StringContent(postBody, Encoding.UTF8, "application/json");
response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
//using (var responseStream = await response.Content.ReadAsStreamAsync())
//{
// using (var reader = new StreamReader(responseStream))
// {
// responseFromAPI = reader.ReadToEnd();
// }
//}
return new JsonTextReader(new StreamReader(await response.Content.ReadAsStreamAsync()));
Appreciate if any one can help us ..
Edit: Please note that we are able to debug it locally and works fine. Only problem when we run this as Worker Role in Azure Cloud service.
I finally addressed this issue. Just to close this (might help someone) -
After doing remote debugging we found the below inner exception :
{"The request was aborted: The request was canceled."}
And, the root cause for this issue is that we did set the timeout to less than what the actual read (JsonTextReader) operation would take. The below line of code which sets the time out :
HttpClient httpClient = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(taskProfileInfo.CommandTimeout) };
So, the FIX is to increase time out value so that request will not be cancelled while reading data.
I fixed increasing the seconds in the web config, it's an alternative:
sessionState timeout="50"

Liferay jsonws not accessible from remote location

My liferay portlet jsonws not accessible from remote location but i can access it using localhost
example [http://localhost:8050/MySite-portlet/api/secure/jsonws] is accesible on the lacal machine
but when i try to access it remotely using the external ip e.g
[http://120.23.223.24:8050/MySite-portlet/api/secure/jsonws] its returning me the Connection refused error
However [http://120.23.223.24:8050/api/jsonws] and [http://120.23.223.24:8050/web/MySite] is working
my portal-ext.properties file contains the following entries
open.id.auth.enabled=
auth.login.site.url=
auth.login.community.url=
company.default.home.url=
default.logout.page.path=
default.landing.page.path=
redirect.url.ips.allowed=
jsonws.servlet.hosts.allowed=
json.servlet.hosts.allowed=
json.web.service.enabled=true
jsonws.web.service.public.methods=*
json.service.auth.token.enabled=true
jsonws.web.service.strict.http.method=false
I am using liferay-tomcat 6.1.0
Below is the error I am getting
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
`java.net.ConnectException: Connection refused: connect
java.net.PlainSocketImpl.socketConnect(Native Metho
d)
java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
java.net.Socket.connect(Socket.java:529)
java.net.Socket.connect(Socket.java:478)
sun.net.NetworkClient.doConnect(NetworkClient.java:163)
sun.net.www.http.HttpClient.openServer(HttpClient.java:394)
sun.net.www.http.HttpClient.openServer(HttpClient.java:529)
sun.net.www.http.HttpClient.<init>(HttpClient.java:233)
sun.net.www.http.HttpClient.New(HttpClient.java:306)
sun.net.www.http.HttpClient.New(HttpClient.java:323)
sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:860)
sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:801)
sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:726)
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1049)
java.net.URL.openStream(URL.java:1010)
com.liferay.portal.jsonwebservice.JSONWebServiceServlet.service(JSONWebServiceServlet.java:136)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
com.liferay.portal.kernel.servlet.PortalClassLoaderServlet.service(PortalClassLoaderServlet.java:98)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:72)
sun.reflect.GeneratedMethodAccessor218.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.liferay.portal.kernel.bean.ClassLoaderBeanHandler.invoke(ClassLoaderBeanHandler.java:54)
$Proxy431.doFilter(Unknown Source)
com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:72)
com.liferay.portal.kernel.servlet.BaseFilter.processFilter(BaseFilter.java:121)
com.liferay.portal.servlet.filters.secure.SecureFilter.processFilter(SecureFilter.java:201)
com.liferay.portal.kernel.servlet.BaseFilter.doFilter(BaseFilter.java:48)
com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:203)
com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:105)
com.liferay.portal.kernel.servlet.PortalClassLoaderFilter.doFilter(PortalClassLoaderFilter.java:69)
com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.processDoFilter(InvokerFilterChain.java:203)
com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:105)
com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:70)`
Can you set up a debugger to look into this? If you look at the code in JSONWebServiceServlet, you'll find this in service() (I took the code from 6.1.1 which I have available - this differs from the line numbers in your sample - you might want to try an update as well, something has changed in this class. Also, for brevity, I've eliminated empty lines and some linebreaks, don't let that irritate you)
if (servletContext.getContext(PropsValues.PORTAL_CTX) != null) {
RequestDispatcher requestDispatcher = request.getRequestDispatcher(
apiPath);
requestDispatcher.forward(request, response);
}
else {
String requestURI = request.getRequestURI();
String requestURL = String.valueOf(request.getRequestURL());
String serverURL = requestURL.substring(0, requestURL.length() - requestURI.length());
String queryString = request.getQueryString();
if (Validator.isNull(queryString)) {
queryString = StringPool.BLANK;
}
else {
queryString += StringPool.AMPERSAND;
}
String servletContextPath = ContextPathUtil.getContextPath(servletContext);
queryString += "contextPath=" + HttpUtil.encodeURL(servletContextPath);
// CHECK THIS VALUE IN DEBUGGER:
apiPath = serverURL + apiPath + StringPool.QUESTION + queryString;
URL url = new URL(apiPath);
InputStream inputStream = null;
try {
inputStream = url.openStream();
OutputStream outputStream = response.getOutputStream();
StreamUtil.transfer(inputStream, outputStream);
}
finally {
StreamUtil.cleanUp(inputStream);
}
}
One possibility is that Liferay itself can't connect to the server name that it determines - e.g. because of Firewall or DNS setup. Once you know what apiPath results in (sadly there seems to be no logging that you could activate) you should be a lot closer to the solution than now.
Set following property in portal-ext.properties
json.servlet.hosts.allowed=
Let me know if you have any problem !!

Resources