Java Google Drive SDK SocketTimeoutException on File.insert.execute - java

I have been using the Google Drive SDK in both Java and Objective-c for months now and suddenly today (Jul 10, 2013) inserting new files using the java api (google-api-client-1.15.0-rc.jar & version 1.14) is failing to execute on FileInsert.execute(). Here is the static helper method I've been using (basically a copy from the DrEdit tutorials):
public static File insertFile(Drive driveService, String title, String description,
String parentId, String mimeType, java.io.File content) {
System.out.println("<GDrive insertFile> start");
File body = new File();
body.setTitle(title);
body.setDescription(description);
body.setMimeType(mimeType);
if (parentId != null && parentId.length() > 0) {
body.setParents( Arrays.asList(new ParentReference().setId(parentId)) );
}
Insert fileInsert = null;
System.out.println("<GDrive insertFile> before content attache");
if (content != null && content.length() > 0) {
FileContent mediaContent = new FileContent(mimeType, content);
try { fileInsert = driveService.files().insert(body, mediaContent); }
catch (IOException e) { e.printStackTrace(); }
} else {
try { fileInsert = driveService.files().insert(body); }
catch (IOException e) { e.printStackTrace(); }
}
try {
if (fileInsert != null) {
System.out.println("<GDrive insertFile> before execute");
File file = fileInsert.execute();
System.out.println("<GDrive insertFile> file posted " + file.getId());
return file;
} else return null;
} catch (IOException e) {
System.out.println("An error occured: " + e);
return null;
}
}
Obviously System.out was used to locate the issue, it hangs up on "before execute". I haven't been able to find any posts about Google server issues or changes in how the api is to be used. Here is the error message that is logged when the app fails any time after 20 - 180 seconds:
An error occured: java.net.SocketTimeoutException: Read timed out
Exception in thread "main" java.lang.NullPointerException
Thanks.

Related

Tesseract OCR not working for java web application

I am trying to develop Java web Tesseract OCR application. Following code works perfectly :
public class App {
public String getImgText(String imageLocation) {
ITesseract instance = new Tesseract();
instance.setDatapath(Thread.currentThread().getContextClassLoader().getResource("tessdata").getPath());
System.out.println("Thread.currentThread().getContextClassLoader().getResource(\"tessdata\").getPath() : "+Thread.currentThread().getContextClassLoader().getResource("tessdata").getPath());
instance.setLanguage("eng");
try {
String imgText = instance.doOCR(new File(imageLocation));
return imgText;
} catch (TesseractException e) {
e.getMessage();
return "Error while reading image";
}
}
public static void main(String[] args) {
App app = new App();
System.out.println(app.getImgText("/home/user/Desktop/1.png"));
}
}
But when I trying to use the above code in my Java web(JSF) application after the line
ITesseract instance = new Tesseract();
nothing is printed out. Below is the code of my web application :
public String uploadImage(FileUploadEvent event) {
System.out.println("webcore bean");
//get uploaded file from the event
UploadedFile uploadedFile = (UploadedFile) event.getFile();
//create an InputStream from the uploaded file
InputStream inputStr = null;
try {
inputStr = uploadedFile.getInputstream();
} catch (IOException e) {
//log error
}
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
String directory = externalContext.getInitParameter("uploadDirectory");
String filename = FilenameUtils.getName(uploadedFile.getFileName());
File destFile = new File(directory, "static" + getFileExtension(filename));
//use org.apache.commons.io.FileUtils to copy the File
try {
FileUtils.copyInputStreamToFile(inputStr, destFile);
} catch (IOException e) {
//log error
}
System.out.println("getImageText(directory) : " + getImageText(directory));
FacesMessage msg = new FacesMessage(event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
return null;
}
private String getImageText(String imageLocation) {
try {
System.out.println("Before ");
ITesseract instance = new Tesseract1();
System.out.println("After ");
//instance.setDatapath("/usr/share/tesseract-ocr/4.00/tessdata");
instance.setDatapath(Thread.currentThread().getContextClassLoader().getResource("tessdata").getPath());
instance.setLanguage("eng");
try {
String imgText = instance.doOCR(new File(imageLocation));
return imgText;
} catch (TesseractException e) {
e.getMessage();
return "Error while reading image";
}
} catch (Exception e) {
System.out.println("Before returning null");
e.printStackTrace();
return null;
}
}
The log "Before" is being printed but the log "After" is not being printed. I am using following technologies :
a) Ubuntu 18.04 64 bit OS
b) Netbeans
c) Maven
d) Glassfish 4.1

Handle java.nio.file.AccessDeniedException

I'm using this code to read value from a file.
public String getChassisSerialNumber() throws IOException
{
File myFile = new File("/sys/devices/virtual/dmi/id/chassis_serial");
byte[] fileBytes;
String content = "";
if (myFile.exists())
{
fileBytes = Files.readAllBytes(myFile.toPath());
if (fileBytes.length > 0)
{
content = new String(fileBytes);
}
else
{
return "No file";
}
}
else
{
return "No file";
}
return null;
}
I get this error:
java.nio.file.AccessDeniedException: /sys/devices/virtual/dmi/id/chassis_serial
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:84)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
at java.nio.file.Files.newByteChannel(Files.java:361)
at java.nio.file.Files.newByteChannel(Files.java:407)
at java.nio.file.Files.readAllBytes(Files.java:3149)
How I can handle this error? Because now I the code stops execution? Is there some better way without interruption the code execution?
You have to use try-catch, either within getChassisSerialNumber() on when calling it. E.g.
try {
getChassisSerialNumber();
} catch (java.nio.file.AccessDeniedException e) {
System.out.println("caught exception");
}
OR
try {
fileBytes = Files.readAllBytes(myFile.toPath());
} catch (java.nio.file.AccessDeniedException e) {
return "access denied";
}
This way your program does not terminate.
For a clean design you should either return null in cases you could not read the file (returning "magic strings like "No file" or "access denied" are no good design, because you cannot differentiate if this string came from the file or not) or catch the exception outside of the method (my first example).
Btw. by just putting the content of the file into the content variable you don't return it (i.e., replace content = new String(fileBytes); with return new String(fileBytes);)
public String getChassisSerialNumber()
{
File myFile = new File("/sys/devices/virtual/dmi/id/chassis_serial");
if (myFile.exists())
{
byte[] fileBytes;
try {
fileBytes = Files.readAllBytes(myFile.toPath());
} catch (java.nio.file.AccessDeniedException e) {
return null;
}
if (fileBytes != null && fileBytes.length > 0)
{
return new String(fileBytes);
}
}
return null;
}
You should catch the exception instead of throwing it. I think that you need to put a try-catch block around the call to the method getChassisSerialNumber.
Something like this should work in your case:
String result = null;
try {
result = getChassisSerialNumber();
} catch (java.nio.file.AccessDeniedException ex) {
// do something with the exception
// you can log it or print some specific information for the user
}
return result; // if the result is null, the method has failed
In order to understand better this kind of things you should have a look to this page

Error in FileUtils.copyUrlToFile

I am executing the below code to pull the .gz file from a URL to a local directory.For small files it goes through fine but for large files it downloads only part of it but does not fail. I get to know the error only when I try to UNZIP it. Can someone throw any light on this on what could be the reason.
public boolean downloadFilemethod(String filePath, String url, String
decompressFilePath) {
if (StringUtils.isNotBlank(filePath) && StringUtils.isNotBlank(url)) {
try {
FileUtils.copyURLToFile(new URL(url), new File(SRC_BASE_DIR + filePath),
TIMEOUT_IN_MILLIS, TIMEOUT_IN_MILLIS);
ingestmethod(filePath,decompressFilePath);
downloadSuccess = true;
}
catch (MalformedURLException e)
{ LOG.warn("some message"); }
catch (IOException e)
{ LOG.warn("some message);
}
}
return downloadSuccess
}

DFC - set a different filestore before save

I have a thread who's updating a map where all the available filestore are declared.
To be available, a filestore must be "online" and his size must be < 500MB. If the filestore reach the 500MB limit, he's turn "Read Only" and a new one is created. That part is OK.
The main thread is attributing a filestore, based on the available ones on the map, to every new document. BUT, I want to handle the case where document is linked to a filestore, let's say the filestore_01, and just between the attribution and the save() method, the filestore_01 is update by the second thread and turned "Read Only".
So, I put a catch and I do a test on the error code to launch a new compute storage to the document if that error occurs. The problem is even if the "new" filestore seems to be linked to my document, when I recall the save() method Documentum retry to save the document in the original filestore, the filestore_01.
I do everything via DFC, I can't use the MIGRATE_JOB as my document is new and not save for the moment.
Anyone has an idea ?
Here's the code :
//Save the document
try {
doc.save();
DfLogger.info(ListenerCreateDocumentOperation.class, "Created document with id '" + doc.getObjectId().getId() + "' and serie '" + serialId + "'", null, null);
} catch (DfException e) {
//if filestore is readonly
if(e.getErrorCode()==256) {
StorageService.getInstance().updateFileStoresArray(); //force to update the filestores map
try {
doc.computeStorageType(); //recompute the filestore where the doc will be save
doc.save(); //save the document
DfLogger.info(ListenerCreateDocumentOperation.class, "Created document with id '" + doc.getObjectId().getId() + "'", null, null);
} catch (Exception e2) {
e.printStackTrace();
throw new DfException("Error - Transaction aborted for XML : " + process.getXmlPath());
}
}
e.printStackTrace();
The first computeStorage() call is inside the setFile() DFC method, where I link a PDF to the document.
And the second thread where the filestore map is update (running approx. every 5 seconds) launch this function :
public void updateFileStoresArray() {
LOGGER.info(StorageService.class+" updating filestore array");
IDfSession s0 = null;
try {
FILESTORE_ARRAY.clear();
s0 = getDctmSessionManager().getSession(getRepository());
s0.addDynamicGroup("dm_superusers_dynamic");
getAllFileStores(s0);
for(int i=0; i<FILESTORE_ARRAY.size(); i++) {
try {
IDfFileStore currentFileStore = FILESTORE_ARRAY.get(i);
if(currentFileStore.getCurrentUse()/1000000 >= max_size) {
LOGGER.info("Filestore "+currentFileStore.getName()+" is full, he'll be set in readonly mode and a new dm_filestore will be create");
FILESTORE_ARRAY.remove(i);
IDfQuery batchList = new DfQuery();
batchList.setDQL("execute set_storage_state with store = '"+currentFileStore.getName()+"', readonly=true");
batchList.execute(s0, IDfQuery.DF_QUERY);
IDfFileStore filestore = createNewFilestore(s0);
FILESTORE_ARRAY.add(filestore);
}
} catch (Exception e) {
DfLogger.error(StorageService.class, "Error in execute()", null, e);
e.printStackTrace();
}
}
if(FILESTORE_ARRAY.size()==0) {
LOGGER.info("Recomputing");
createNewFilestore(s0);
}
}
catch (DfException e1) {
e1.printStackTrace();
}
finally {
if (s0 != null) {
getDctmSessionManager().release(s0);
}
}
}
And here's the computeStorage() method :
public void computeStorageType() throws DfException {
if(getStorageType()==null || getStorageType().equals("filestore_01") || Utils.isNullString(getStorageType())) {
getSession().addDynamicGroup("dm_superusers_dynamic");
String storageType=null;
StorageService.getInstance();
storageType = StorageService.computeStorage(getSession(), this);
if(getStorageType()==null || !getStorageType().equals(storageType)) {
setStorageType(storageType);
}
getSession().removeDynamicGroup("dm_superusers_dynamic");
}
}
public static String computeStorage(IDfSession s0, IGenericAspect vfkGenericDocumentAspect) throws DfException {
String result = null;
try {
if(FILESTORE_ARRAY.size()==0) {
getAllFileStores(s0);
}
if(FILESTORE_ARRAY.size()==0) {
createNewFilestore(s0);
getAllFileStores(s0);
}
IDfFileStore filestore = FILESTORE_ARRAY.get(currentFileStoreIndex);
if(filestore.getStatus()==2 || filestore.getStatus()==1) {
if(currentFileStoreIndex+1<FILESTORE_ARRAY.size() && FILESTORE_ARRAY.get(currentFileStoreIndex+1)!=null) {
currentFileStoreIndex=currentFileStoreIndex+1;
filestore = FILESTORE_ARRAY.get(currentFileStoreIndex);
}
}
result= filestore.getName();
LOGGER.info("Document "+vfkGenericDocumentAspect.getObjectId()+" will be store in filestore "+result);
if(currentFileStoreIndex+1<FILESTORE_ARRAY.size())
currentFileStoreIndex=currentFileStoreIndex+1;
else
currentFileStoreIndex=0;
} catch(Exception e) {
DfLogger.error(StorageService.class, "Error in execute()", null, e);
e.printStackTrace();
}
return result;
}

Why Java return statment inside the catch block not working?

Why does the following code always return true even when an exception is thrown?
public boolean write (ArrayList<String> inputText, String locationToSave){
try {
File fileDir = new File(locationToSave);
Writer out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(fileDir), "utf8"));
int index = 0;
int size = inputText.size();
while (index < size) {
out.append(inputText.get(index));
out.append("\n");
index++;
}
out.flush();
out.close();
return true;
} catch (UnsupportedEncodingException e) {
System.out.println("UnsupportedEncodingException is : \n" + e.getMessage());
return false;
} catch (IOException e) {
System.out.println("IOException is : \n" + e.getMessage());
return false;
} catch (Exception e) {
System.out.println("Exception is : \n" + e.getMessage());
return false;
}
}
Edition 01
This is the code I'm using to test the previous code:
if (fileReader.write(fileReader.read(selectedFile), selectedSaveLocation)) {
System.out.println("The file : " + selectedFile + " as been successfully"
+ "converted to : " + selectedSaveLocation );
} else {
System.out.println("The file : " + selectedFile + " failed to convert!" );
}
I don't think you're seeing what you think you're seeing. In other words, I'm pretty sure it's actually returning false, and that you should check the calling code.
For example, I pasted your code into a new Java console app, made it static, and wrote a main method with this body:
System.out.println(write(null, null));
The output was:
Exception is :
null
false
It does not always return true. I've created a testproject, caused an IOException ... and get false! There must be an error in your reasoning.
If you're seeing an exception in the console, and the return value is still true, then check the type of exception. Since you catch Exception, I'd guess that it could be a non-checked Throwable that's being triggered. You wouldn't ever set the flag to false in that case.
I might write it this way:
public boolean write (Collection<String> inputText, String locationToSave)
{
boolean isSuccessful = false;
Writer out;
try
{
File fileDir = new File(locationToSave);
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(fileDir), "utf8"));
for (String line : inputText)
{
out.append(inputText.get(index));
out.append("\n");
}
isSuccessful = true;
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
cleanup(out);
}
return isSuccessful;
}
private static void cleanup(Writer out)
{
try
{
if (out != null)
{
out.flush();
out.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
As everyone already said, the exception is not the one you think it is. I would guess the method
fileReader.read(selectedFile)
logs the exception you see in your logs...
Show us the code of this method... And also show us the exception...

Categories

Resources