GoogleTransport class problem - java

i'm using a maven project with following dependency :
<dependency>
<groupId>com.google.api.client</groupId>
<artifactId>google-api-client-googleapis-auth-clientlogin</artifactId>
<version>1.2.3-alpha</version>
</dependency>
when i run following code:
import java.io.IOException;
import com.google.api.client.googleapis.GoogleTransport;
import com.google.api.client.googleapis.auth.clientlogin.ClientLogin;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpResponseException;
import com.google.api.client.http.HttpTransport;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ) throws IOException
{
HttpTransport transport = GoogleTransport.create();
// transport.addParser(new JsonCParser());
try {
// authenticate with ClientLogin
ClientLogin authenticator = new ClientLogin();
authenticator.authTokenType = "ndev";
authenticator.username = "....";
authenticator.password = "....";
authenticator.authenticate().setAuthorizationHeader(transport);
// make query request
HttpRequest request = transport.buildGetRequest();
request.setUrl("https://www.googleapis.com/bigquery/v1/query");
request.url.put(
"q", "select count(*) from [bigquery/samples/shakespeare];");
System.out.println(request.execute().parseAsString());
} catch (HttpResponseException e) {
System.err.println(e.response.parseAsString());
throw e;
}
}
}
i get below exception:
Exception in thread "main" java.lang.IllegalStateException: Missing required low-level HTTP transport package.
Use package "com.google.api.client.javanet".
at com.google.api.client.http.HttpTransport.useLowLevelHttpTransport(HttpTransport.java:129)
at com.google.api.client.http.HttpTransport.<init>(HttpTransport.java:187)
at com.google.api.client.googleapis.GoogleTransport.create(GoogleTransport.java:58)
at com.example.clientlogin.App.main(App.java:18)
what is the problem with GoogleTransport class?

Quick googeling resulted in maven for com.google.api.client.javanet.nethttpresponse Try adding
<dependency>
<groupId>com.google.api.client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.2.3-alpha</version>
</dependency>
or
<dependency>
<groupId>com.google.api.client</groupId>
<artifactId>google-api-client-javanet</artifactId>
<version>1.2.3-alpha</version>
</dependency>
to your POM file

this question is pretty old, but I've added some updates to our Java Google Client Lib + BigQuery samples (here: http://code.google.com/p/google-bigquery-tools/source/browse/samples/java/gettingstarted/BigQueryJavaGettingStarted/).

Related

Expose metrics with akka-http-metrics in java

I'm trying expose metrics to Prometheus with library https://github.com/RustedBones/akka-http-metrics in java project.
After adapted code to java, I dont receive http metrics after call method, only a empy response.
If add module for jvm I have only jvm metrics.
package test;
import akka.http.javadsl.model.StatusCodes;
import akka.http.javadsl.server.AllDirectives;
import akka.http.javadsl.server.Route;
import akka.http.javadsl.server.directives.RouteAdapter;
import fr.davit.akka.http.metrics.core.scaladsl.server.HttpMetricsDirectives$;
import fr.davit.akka.http.metrics.prometheus.PrometheusRegistry;
import fr.davit.akka.http.metrics.prometheus.PrometheusSettings;
import fr.davit.akka.http.metrics.prometheus.marshalling.PrometheusMarshallers$;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.hotspot.DefaultExports;
import static akka.http.javadsl.server.PathMatchers.segment;
public class TestHttpMetrics extends AllDirectives {
public Route createRoute() {
return pathPrefix(segment("v1").slash("metrics"),
() -> concat(
path(segment("prometheus"), () -> get(this::micrometer)),
complete(StatusCodes.NOT_FOUND, "Path not found")
)
);
}
public Route micrometer() {
return pathEnd(() -> {
try {
CollectorRegistry prometheus = new CollectorRegistry();
PrometheusSettings settings = new MyPrometheusSettings().getInstance();
PrometheusRegistry registry = new PrometheusRegistry(settings, prometheus);
//DefaultExports.register(prometheus); //for JVM metrics
return RouteAdapter.asJava(HttpMetricsDirectives$.MODULE$.metrics(registry, PrometheusMarshallers$.MODULE$.marshaller()));
} catch (Exception e) {
e.printStackTrace();
}
return complete(StatusCodes.INTERNAL_SERVER_ERROR, "ERROR");
});
}
}
class MyPrometheusSettings {
public PrometheusSettings getInstance() throws Exception {
PrometheusSettings ps = ((PrometheusSettings) PrometheusSettings.class.getDeclaredMethod("default").invoke(null)) //default is reserved in java!
.withNamespace("akka_http")
.withIncludePathDimension(true)
.withIncludeMethodDimension(true)
.withIncludeStatusDimension(true)
.withDurationConfig(PrometheusSettings.DurationBuckets())
.withReceivedBytesConfig(PrometheusSettings.DefaultQuantiles())
.withSentBytesConfig(PrometheusSettings.DefaultQuantiles())
.withDefineError(response -> response.status().isFailure());
return ps;
}
}
in pom
<dependency>
<groupId>com.typesafe.akka</groupId>
<artifactId>akka-http_2.12</artifactId>
<version>10.2.4</version>
</dependency>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>2.12.13</version>
</dependency>
<dependency>
<groupId>fr.davit</groupId>
<artifactId>akka-http-metrics-prometheus_2.12</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>0.15.0</version>
</dependency>
Where is the problem? In debug mode there is only null values in registry.

hadoop distcp via java resulting in NoClassDefFoundError: Could not initialize class com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystem

I am trying to run a distcp command on my hadoop cluster using the Hadoop Java Library to move content from the HDFS to a Google Cloud Bucket. I am getting the error NoClassDefFoundError: Could not initialize class com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystem
Below is my java code:
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.tools.DistCp;
import org.apache.hadoop.tools.DistCpOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class HadoopHelper {
private static Logger logger = LoggerFactory.getLogger(HadoopHelper.class);
private static final String FS_DEFAULT_FS = "fs.defaultFS";
private final Configuration conf;
public HadoopHelper(String hadoopUrl) {
conf = new Configuration();
conf.set(FS_DEFAULT_FS, "hdfs://" + hadoopUrl);
}
public void distCP(JsonArray files, String target) {
try {
List<Path> srcPaths = new ArrayList<>();
for (JsonElement file : files) {
String srcPath = file.getAsString();
srcPaths.add(new Path(srcPath));
}
DistCpOptions options = new DistCpOptions.Builder(
srcPaths,
new Path("gs://" + target)
).build();
logger.info("Using distcp to copy {} to gs://{}", files, target);
this.conf.set("fs.gs.impl", "com.google.cloud.hadoop.fs.gcs.GoogleHadoopFileSystem");
this.conf.set("fs.gs.auth.service.account.email", "my-svc-account#my-gcp-project.iam.gserviceaccount.com");
this.conf.set("fs.gs.auth.service.account.keyfile", "config/my-svc-account-keyfile.p12");
this.conf.set("fs.gs.project.id", "my-gcp-project");
DistCp distCp = new DistCp(this.conf, options);
Job job = distCp.execute();
job.waitForCompletion(true);
logger.info("Distcp operation success. Exiting");
} catch (Exception e) {
logger.error("Error while trying to execute distcp", e);
logger.error("Distcp operation failed. Exiting");
throw new IllegalArgumentException("Distcp failed");
}
}
public void createDirectory() throws IOException {
FileSystem fileSystem = FileSystem.get(this.conf);
fileSystem.mkdirs(new Path("/user/newfolder"));
logger.info("Done");
}
}
I have added the below dependencies in the pom.xml:
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-distcp</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.google.cloud.bigdataoss</groupId>
<artifactId>gcs-connector</artifactId>
<version>hadoop3-2.2.4</version>
</dependency>
<dependency>
<groupId>com.google.cloud.bigdataoss</groupId>
<artifactId>util</artifactId>
<version>2.2.4</version>
</dependency>
If I run the distcp command on the cluster itself like so: hadoop distcp /user gs://my_bucket_name/
The distcp operation works and the content is copied onto the Cloud Bucket.
Did you add the jar to the hadoop's classpath?
Add the connector jar to Hadoop's classpath
Placing the connector jar in the HADOOP_COMMON_LIB_JARS_DIR directory should be sufficient to have Hadoop load the jar. Alternatively, to be certain that the jar is loaded, you can add HADOOP_CLASSPATH=$HADOOP_CLASSPATH:</path/to/gcs-connector.jar> to hadoop-env.sh in the Hadoop configuration directory.
This needs to be done to DisctCp conf(in your code this.conf) before this line of code:
this.conf.set("HADOOP_CLASSPATH","$HADOOP_CLASSPATH:/tmp/gcs-connector-latest-hadoop2.jar")
DistCp distCp = new DistCp(this.conf, options);
If it helps there is a troubleshooting section for this.

How do I solve an io.netty error in my testing project

I am setting up a back-end test using Java.
When running my test I am presented with the following error:
java.lang.NoSuchMethodError: io.netty.util.internal.PlatformDependent.allocateUninitializedArray(I)[B
My pom file contains the following dependencies in regard to Netty:
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-transport</artifactId>
<version>4.1.36.Final</version>
</dependency>
My code itself looks as follows:
import cucumber.api.java.en.And;
import org.mockserver.client.MockServerClient;
import org.mockserver.matchers.Times;
import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PdfGenerateStep {
#Autowired
private MockServerClient mockServerClient;
#And("Pdf {string} is generated")
public void generatePDF(String pdfFile) {
HttpRequest httpRequest = new HttpRequest();
httpRequest.withPath("/pdf-service/doc/request")
.withHeader("template", "TEST")
.withHeader("docFormat", "pdf")
.withHeader("fromParty", "PDFGEN")
.withHeader("APPLICATION", "App")
.withMethod("POST");
HttpResponse httpResponse = new HttpResponse();
httpResponse.withStatusCode(200);
httpResponse.withBody(readPdfFile(pdfFile));
mockServerClient.when(httpRequest, Times.once()).respond(httpResponse);
}
private byte[] readPdfFile(String file) {
try {
Path path = Paths.get(getClass().getClassLoader().getResource(file).toURI());
return Files.readAllBytes(path);
} catch (URISyntaxException | IOException e) {
e.printStackTrace();
}
return null;
}
}
There is no allocateUninitializedArray method in io.netty.util.internal.PlatformDependent class, so in your classpath there is an another jar that contains this class, but the version, and therefore the code of this class will be different.
The io.netty.util.internal.PlatformDependent class can be found in netty-common, which is a transitive dependency of netty-transport.
So, check the dependency tree of your project.
Very probably you have an another dependency that has a different version of netty-common as a transitive dependency.
Exclude the wrong one and you will be done.
<dependency>
<groupId>com.corundumstudio.socketio</groupId>
<artifactId>netty-socketio</artifactId>
<version>1.7.13</version>
</dependency>
add this dependency in your pom.xml file.

XML-RPC to connect confluence in JAVA

I have to use XML-RPC to upload a picture to confluence in JAVA.
JAVA CODE
import java.util.Vector;
import java.util.Hashtable;
import helma.xmlrpc.*;
import java.util.Arrays;
public class test {
// The location of our server.
private final static String server_url =
"http://confluence.xyz.com:8080/rpc/xmlrpc";
public static void main (String [] args) {
try {
// Create an object to represent our server.
XmlRpcClient server = new XmlRpcClient(server_url);
Vector<Object> params = new Vector<Object>();
params.add("username");
params.add("pass");
String token = (String) server.execute("confluence2.login", params );
System.out.println(token);
}
catch (Exception exception) {
System.err.println("JavaClient: " + exception.toString());
}
}
}
I am getting error
JavaClient: java.io.IOException: SAX driver not found: org.apache.xerces.parsers.S
You need to put additional xercesImpl-2.2.1.jar for this. If you were using maven ,you can just simply add xercesImpl maven dependency as below:
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.2.1</version>
</dependency>
Hope it helps!

java.lang.NoClassDefFoundError: Could not initialize class javax.mail.internet.InternetAddress

I am trying to send an email using the JavaMail API. Here is my code on the servlet:
package com.lsp.web;
import com.lsp.service.Mailer;
import javax.ejb.EJB;
import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
#WebServlet(name = "contact", urlPatterns = {"/contact"})
public class ContactServlet extends SpringInjectedServlet {
#EJB
private Mailer emailBean;
#Override
public void init() throws ServletException {
}
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String customerEmail = req.getParameter("email");
String subject = req.getParameter("subject");
String body = req.getParameter("message");
String error = null;
String succMess = null;
try {
javax.mail.internet.InternetAddress ia = new javax.mail.internet.InternetAddress(customerEmail);
ia.validate();
emailBean.send(customerEmail, subject, body);
req.setAttribute("succMessage", succMess);
req.getRequestDispatcher("sent.jsp").forward(req, resp);
} catch (javax.mail.internet.AddressException ae) {
error = "您指出的邮箱地址不存在";
req.setAttribute("errorMessage", error);
req.getRequestDispatcher("contact.jsp").forward(req, resp);
}
catch (MessagingException mex) {
error = "发送失败";
req.setAttribute("errorMessage", error);
req.getRequestDispatcher("contact.jsp").forward(req, resp);
}
}
}
At the line where I check for the user address where:
javax.mail.internet.InternetAddress ia = new javax.mail.internet.InternetAddress(customerEmail);
ia.validate();
I got an exception.
java.lang.NoClassDefFoundError: Could not initialize class javax.mail.internet.InternetAddress
In pom.xml, I added these lines:
<!--JavaMail API-->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.1</version>
</dependency>
<!--EJB-->
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>ejb-api</artifactId>
<version>3.0</version>
</dependency>
I am using Tomcat.
Could someone tell me why this happens and how I can solve the issue.
Thank you.
Please see: https://stackoverflow.com/a/28935760/1128668 You have included the mail-api.jar in your project. That's the API specification only. The fix is to replace this:
<!-- DO NOT USE - it's just the API, not an implementation -->
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
with the reference implementation of that api:
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
I know it has sun in the package name, but that's the latest version.
You get a java.lang.NoClassDefFoundError, which means that the JVM can't initialise the class, not that it can't find the class which would be a ClassNotFoundException. A NoClassDefFoundError can be caused by a ClassNotFoundException but that need not be the case.
In order to find the cause, stop the server, delete the log and start again. Then reproduce the error and try to find the first Exception and its cause in your log file. If you are lucky this is the cause for the NoClassDefFoundError.
You also might indicate in your question which server you are using. It might make a difference how to solve the error.
Adding the dependency at build time does nothing to make the dependency available to Tomcat at runtime. You need to add the javax.mail.jar file to the WEB-INF/lib directory of your application, or to Tomcat's lib directory.
Of course, you wouldn't have this problem if you were using a full Java EE application server instead of Tomcat... :-)

Categories

Resources