I am trying to convert Http upload to use the new HttpClient 4.3.1 classes. I'm new to Java. Everything I find online uses deprecated classes (i.e. HttpClient client = new DefaultHttpClient() or an even older method for creating an instance of HttpClient. Forgive all the extra libraries below, some will be needed in the rest of my project.
I've tried umpteen different ways to create the instance, what I have below is the method I see used in org.appache documenation for 4.3.1.
Unfortunately, I'm getting an error indicating that HttpClientBuilder is not visible. I'm not even sure what not visible means...the library has been imported. Can anyone point me in the right direction for creating an HttpClient instance.
package newHttpApiUpload;
import org.apache.http.client.HttpClient;
import org.apache.http.HttpConnection;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.AbstractHttpEntity;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class Api {
protected static final HttpClientBuilder client = new HttpClientBuilder();
}
The constructor HttpClientBuilder() is protected (not public) so you cannot call it from your code. That is what not visible means.
You invoke the builder using a static constructor method, as such:
HttpClientBuilder.create();
Or, to quickly create a client (which is the whole point)
HttpClient client = HttpClientBuilder.create()
.setUserAgent("MyAgent")
.setMaxConnPerRoute(4)
.build()
You need to use static factory methods from HttpClients.
You can use them to obtain preconfigured instances of HttpClient, or use HttpClients.custom() to apply your custom configuration using HttpClientBuilder.
Related
Is there any built-in library exists or is it required to implement custom one?
I have tried to check here, but not sure how to ahead from here: Bigtable connection pool
I have tried the below code, but not really sure how to progress from here:
import com.google.cloud.bigtable.config.BigtableOptions;
import com.google.cloud.bigtable.config.CredentialOptions;
import com.google.cloud.bigtable.grpc.BigtableSession;
import com.google.cloud.bigtable.grpc.io.ChannelPool;
import com.mahindra.digisense.config.AppConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
#Component
public class BigTableConnectionPoolingExample {
#Autowired
private AppConfig.BigTableConfig bigTableConfig;
private void bigTableConnectionPooling() throws IOException, GeneralSecurityException {
CredentialOptions credentialOptions = CredentialOptions.jsonCredentials(new FileInputStream(new File(bigTableConfig.getCredentialsJson())));
BigtableOptions.Builder builder = new BigtableOptions.Builder();
builder.setCredentialOptions(credentialOptions);
ChannelPool.ChannelFactory channelFactory = (ChannelPool.ChannelFactory) BigtableSession.createChannelPool(bigTableConfig.getInstanceId(), builder.build());
ChannelPool channelPool = new ChannelPool(channelFactory,3);
}
}
Here is another Stack Overflow question, which has no answers.
As noted by Solomon Duskis, we are encouraging new folks to get started with the idiomatic Bigtable client in google-cloud-java. The client is suitable for production use, however we have not finalized the client API so we may make backward-incompatible changes.
If you are using the HBase client from the Cloud Bigtable Client repo, there are options to adjust the number of data channels used underneath as well as the number of inflight RPCs per channel. But we suggest that you profile your application first, as you should be able to achieve good performance and saturate your clusters without needing to manually adjust these parameters from their defaults.
Specifically, I am trying to enable .SVG files to be usable by the core image component.
Right now I am making a sling model that ideally I would like to access the returned values of the getSrc() and getFileReference() classes in the core AEM Component interface located here.
I am very new to AEM development and Sling models. Am I missing some vital functionality that would let me do this?
Here is my code, which probably isn't at all helpful at this point.
package com.site.core.models;
import com.adobe.cq.wcm.core.components.models.Image;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.*;
import org.apache.sling.models.annotations.injectorspecific.*;
import org.apache.sling.settings.SlingSettingsService;
import javax.jcr.Node;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.inject.Inject;
#Model(adaptables = SlingHttpServletRequest.class)
public class ImageModel {
private String src = Image.getSrc();
return src;
}
As I mentioned in my comment, the link you are referring to is an interface, the implementation of that interface is here
In order to use your own implementation, you have two options:
In the image component html, change the data-sly-use to refer to your impl: com.site.core.models.ImageModel
Create a separate model that implements the Image interface and give it a high ranking to be picked up instead of the existing impl.
Disclaimer: I have not tested #2 but the documentation suggests that it's possible.
I'm using json4s in a play project, and I'm also using a library called sbt-buildinfo which generates Scala source from your build definitions.
Now, in the sbt-buildinfo library the say you need to add some line of code: buildInfoOptions += BuildInfoOption.ToJson so you can use .toJson, but from some reason I can use .toJson.
this is how I do it:
import _root_.util.{AuthenticatedAction}
import buildinfo.BuildInfo
import com.google.inject.Inject
import org.json4s.BuildInfo
import play.api._
import play.api.mvc._
class AppInfo #Inject()(implicit configuration: Configuration) extends Controller {
def appVerion = AuthenticatedAction {
Ok(BuildInfo.toJson)
}
but the import buildinfo.BuildInfo stays gray....so it looks like I'm not using it. I refreshed the build.sbt and all, what could it be?
You have multiple imports to a BuildInfo object. org.json4s.BuildInfo will probably shadow your buildInfo.BuildInfo import and therefore, it does not have the required member. Try writing out the entire package name that you need:
Ok(buildinfo.BuildInfo.toJson)
I am getting following error in jsp page for file upload code:
The method parseRequest(RequestContext) in the type FileUploadBase is not applicable for the arguments (HttpServletRequest)
error in the code:
List<FileItem> items = uploadHandler.parseRequest(request);
The parseRequest(RequestContext ctx) expects RequestContext instance as argument but the argument passed is instance of HttpServletRequest
Use ServletRequestContext to create a RequestContext instance as follows.
List<FileItem> items
= uploadHandler.parseRequest(new ServletRequestContext(request));
I had same problem, then found my imports were wrong: the last one of them was using fileupload from sun, not from commons.fileupload. After I changed them all to commons.fileupload, the error disappeared:
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.servlet.ServletRequestContext;
great answers above, but if you are upgrading to tomcat10 which naming change from javax to jakarta, commons-fileupload as of version 1.4 has not change the naming yet, but you can change to the custom class in tomcat10 instead! (LUCKY ME)
org.apache.commons.fileupload.ProgressListener to org.apache.tomcat.util.http.fileupload.ProgressListener
org.apache.commons.fileupload.servlet.ServletFileUpload to org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload
org.apache.commons.fileupload.disk.DiskFileItemFactory to org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory
org.apache.commons.fileupload.FileItem to org.apache.tomcat.util.http.fileupload.FileItem
In a JAVA bean that I am working on I want to pass a NotesXspDocument (could use a NotesDocument) to a method which looks like this:
public List<String> getReaders(NotesXspDocument thisXspDoc){
// do some stuff
}
But JAVA does not recognize the NotesXspDocument definition. I have imported the following packages:
import lotus.domino.NotesException;
import lotus.domino.Session;
import lotus.domino.Database;
import lotus.domino.View;
import lotus.domino.Document;
Is there a further package to import to make use the NotesXspDocument?
To elaborate on Jesses answer: in your case you need to do this to work with the XPages version of Document:
import com.ibm.xsp.model.domino.wrapped.DominoDocument;
public List<String> getReaders(DominoDocument thisXspDoc){
// do some stuff
}
NotesXspDocument is an SSJS-only alias; the real class is com.ibm.xsp.model.domino.wrapped.DominoDocument: http://public.dhe.ibm.com/software/dw/lotus/Domino-Designer/JavaDocs/DesignerAPIs/com/ibm/xsp/model/domino/wrapped/DominoDocument.html