Issue with installation of GWT 2.6 - java

I have just installed GWT 2.6 and am now getting the error "Description Resource Path Location Type
The file war\WEB-INF\lib\gwt-servlet.jar has a different size than GWT SDK library gwt-servlet.jar; perhaps it is a different version? gwt-servlet.jar /AwardTracker/war/WEB-INF/lib Unknown Google Web Toolkit Problem"
I downloaded the GWT 2.6 zip and then copied the directory "GWT-2.6.0" into "Eclipse\eclipse-jee-juno-SR1-win32\eclipse\plugins". I then right clicked on the project and selected "properties/Google/Web Toolkit/Configure SDKs.../Add". I then browsed to the "GWT-2.6.0" directory, added it and selected it.
I followed the solution from Braj and received the following errors when I recompiled:
Compiling module org.AwardTracker.AwardTracker
Validating units:
Ignored 2 units with compilation errors in first pass.
Compile with -strict or with -logLevel set to TRACE or DEBUG to see all errors.
Computing all possible rebind results for 'gwtupload.client.DecoratedFileUpload.DecoratedFileUploadImpl'
Rebinding gwtupload.client.DecoratedFileUpload.DecoratedFileUploadImpl
Could not find an exact match rule. Using 'closest' rule based on fall back values. You may need to implement a specific binding in case the fall back behavior does not replace the missing binding
[ERROR] Errors in 'gwtupload/client/DecoratedFileUpload.java'
[ERROR] Line 347: Rebind result 'gwtupload.client.DecoratedFileUpload.DecoratedFileUploadImpl' cannot be abstract
The above was fixed by downloading gwtupload-1.0.1.jar, using 'Add External JARS' to add it to the library and removing the old gwtupload-0.6.6.jar. I then recompiled and the compile work. However, now I have an error in my "MyCustomisedUploadServlet" on the line (this error was not present before):
protected static final String XML_ERROR_ITEM_NOT_FOUND = "<" + TAG_ERROR + ">item not found</" + TAG_ERROR + ">";
The rest of the code is:
package org.AwardTracker.server;
import gwtupload.server.UploadAction;
import gwtupload.server.exceptions.UploadActionException;
import gwtupload.shared.UConsts;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
/**
* This is an example of how to use UploadAction class.
*
* This servlet saves all received files in a temporary folder,
* and deletes them when the user sends a remove request.
*
* #author Manolo Carrasco Moñino
*
*/
public class MyCustomisedUploadServlet extends UploadAction {
private static final long serialVersionUID = 1L;
protected static final String XML_ERROR_ITEM_NOT_FOUND = "<" + TAG_ERROR + ">item not found</" + TAG_ERROR + ">";
Hashtable<String, String> receivedContentTypes = new Hashtable<String, String>();
/**
* Maintain a list with received files and their content types.
*/
Hashtable<String, File> receivedFiles = new Hashtable<String, File>();
/**
* Override executeAction to save the received files in a custom place
* and delete this items from session.
*/
#Override
public String executeAction(HttpServletRequest request, List<FileItem> sessionFiles) throws UploadActionException {
String response = "";
#SuppressWarnings("unused")
int cont = 0;
for (FileItem item : sessionFiles) {
if (false == item.isFormField()) {
cont ++;
try {
/// Create a temporary file placed in the default system temp folder
File file = File.createTempFile("upload-", ".bin");
item.write(file);
/// Save a list with the received files
receivedFiles.put(item.getFieldName(), file);
receivedContentTypes.put(item.getFieldName(), item.getContentType());
/// Send a customised message to the client.
response += file.getAbsolutePath();
} catch (Exception e) {
throw new UploadActionException(e);
}
}
}
/// Remove files from session because we have a copy of them
removeSessionFileItems(request);
/// Send your customised message to the client.
return response;
}
/**
* Get the content of an uploaded file.
*/
#Override
public void getUploadedFile(HttpServletRequest request, HttpServletResponse response) throws IOException {
String fieldName = request.getParameter(UConsts.PARAM_SHOW);
File f = receivedFiles.get(fieldName);
if (f != null) {
response.setContentType(receivedContentTypes.get(fieldName));
FileInputStream is = new FileInputStream(f);
copyFromInputStreamToOutputStream(is, response.getOutputStream());
} else {
renderXmlResponse(request, response, XML_ERROR_ITEM_NOT_FOUND);
}
}
/**
* Remove a file when the user sends a delete request.
*/
#Override
public void removeItem(HttpServletRequest request, String fieldName) throws UploadActionException {
File file = receivedFiles.get(fieldName);
receivedFiles.remove(fieldName);
receivedContentTypes.remove(fieldName);
if (file != null) {
file.delete();
}
}
}
I simply commented this line out (" protected static final String XML_ERROR_ITEM_NOT_FOUND = "<" + TAG_ERROR + ">item not found";"), recompiled and ran it and it works fine. I hope all this helps others. Thank you to Braj for his help.

This works for me very well
Select the warning, right click and choose
"Quick fix" -> "Synchronize /WEB-INF/lib with SDK libraries"
"Done"

Whenever you change the GWT version you have to clean your project from the stub that is auto-generated by the previous GWT version as shown in below screenshots.
Problem: The file war\WEB-INF\lib\gwt-servlet.jar has a different size than GWT SDK library gwt-servlet.jar; perhaps it is a different version?
Solution: The problem in your case is gwt-servlet.jar that is auto-generated by previous GWT version. Just delete it along with other stub and re-compile project again.

To copy the sdk in the plugin directory is not the corret way to add another SDK version.
Use Preferences -> Google -> Web Toolkit to add another GWT SDK version.
In your project choose:
project properties -> Goolge -> Web Toolkit you can choose the SDK the project should use.

I changed my compiler from 1.8 to 1.7 and lost this message.

Related

Add a file inside a folder to Github using JGit or EGit - directory1\myfile.txt

*Added final working program to the bottom of this question *
I was able to add a file to GitHub using org.eclipse.egit.github.core java library (and code sample is referred from here: https://gist.github.com/Detelca/2337731 )
But I am not able to give a path like "folder1\myfile.txt" OR "folder1\folder2\myfile.txt"
I am trying to find a simple example to add a file under a folder and was not really able to find it.
Any help on this with an example?
Below is the code i am using: (configuration details like username, reponame are in the method addFileToGH() )
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import org.eclipse.egit.github.core.Blob;
import org.eclipse.egit.github.core.Commit;
import org.eclipse.egit.github.core.CommitUser;
import org.eclipse.egit.github.core.Reference;
import org.eclipse.egit.github.core.Repository;
import org.eclipse.egit.github.core.RepositoryCommit;
import org.eclipse.egit.github.core.Tree;
import org.eclipse.egit.github.core.TreeEntry;
import org.eclipse.egit.github.core.TypedResource;
import org.eclipse.egit.github.core.User;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.service.CommitService;
import org.eclipse.egit.github.core.service.DataService;
import org.eclipse.egit.github.core.service.RepositoryService;
import org.eclipse.egit.github.core.service.UserService;
public class GHFileWriter {
public static void main(String[] args) {
try {
new GHFileWriter().addFileToGH("myfile.txt", "some file content");
//new GHFileWriter().addFolderToGH("folder1");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean addFileToGH(String fileName, String fileContent) throws IOException{
String userName = "myusername";
String password = "mypassword";
String repoName = "myrepository";
String ownerName = "myusername";
String email = "myemail#gmail.com";
//Basic authentication
GitHubClient client = new GitHubClient();
client.setCredentials(userName, password);
// create needed services
RepositoryService repositoryService = new RepositoryService();
CommitService commitService = new CommitService(client);
DataService dataService = new DataService(client);
// get some sha's from current state in git
Repository repository = repositoryService.getRepository(ownerName, repoName );
String baseCommitSha = repositoryService.getBranches(repository).get(0).getCommit().getSha();
RepositoryCommit baseCommit = commitService.getCommit(repository, baseCommitSha);
String treeSha = baseCommit.getSha();
// create new blob with data
Blob blob = new Blob();
blob.setContent( fileContent ).setEncoding(Blob.ENCODING_UTF8); //"[\"" + System.currentTimeMillis() + "\"]").setEncoding(Blob.ENCODING_UTF8);
String blob_sha = dataService.createBlob(repository, blob);
Tree baseTree = dataService.getTree(repository, treeSha);
Collection<TreeEntry> entries = new ArrayList<TreeEntry>();
// create new tree entry
TreeEntry treeEntry = new TreeEntry();
treeEntry.setPath( fileName );
treeEntry.setMode(TreeEntry.MODE_BLOB);
treeEntry.setType(TreeEntry.TYPE_BLOB);
treeEntry.setSha(blob_sha);
treeEntry.setSize(blob.getContent().length());
/* //Code for adding folder, not working
TreeEntry folderEntry = new TreeEntry();
folderEntry.setPath( "folder1" );
folderEntry.setMode(TreeEntry.MODE_DIRECTORY);
folderEntry.setType(TreeEntry.TYPE_TREE);
folderEntry.setSha(blob_sha); //one of the possible issues
//folderEntry.setSize(blob.getContent().length());
entries.add(folderEntry);
*/
entries.add(treeEntry);
Tree newTree = dataService.createTree(repository, entries, baseTree.getSha()); //Issue is popped-up here, error is thrown here
// create commit
Commit commit = new Commit();
commit.setMessage("Test commit at " + new Date(System.currentTimeMillis()).toLocaleString());
commit.setTree(newTree);
//Due to an error with github api we have to to all this
//TODO: Make this better (another function)
UserService userService = new UserService(client);
User user = userService.getUser();
CommitUser author = new CommitUser();
Calendar now = Calendar.getInstance();
author.setName( userName );
author.setEmail(email);
//author.setEmail(userService.getEmails().get(0));
author.setDate(now.getTime());
commit.setAuthor(author);
commit.setCommitter(author);
List<Commit> listOfCommits = new ArrayList<Commit>();
listOfCommits.add(new Commit().setSha(baseCommitSha));
// listOfCommits.containsAll(base_commit.getParents());
commit.setParents(listOfCommits);
// commit.setSha(base_commit.getSha());
Commit newCommit = dataService.createCommit(repository, commit);
// create resource
TypedResource commitResource = new TypedResource();
commitResource.setSha(newCommit.getSha());
commitResource.setType(TypedResource.TYPE_COMMIT);
commitResource.setUrl(newCommit.getUrl());
//System.out.println("Committed file URL: "+ newCommit.getUrl());
// get master reference and update it
Reference reference = dataService.getReference(repository, "heads/master");
reference.setObject(commitResource);
dataService.editReference(repository, reference, true);
if( newCommit.getUrl() != null || !newCommit.getUrl().equalsIgnoreCase("") ){
return true;
}
//push.setCommits(commits);
return false;
}
}
Below is the error when i execute the above code.
org.eclipse.egit.github.core.client.RequestException: tree.sha
a3b93733a6dfd221c24e94d2ce52c25307910d73 is not a valid tree (422) at
org.eclipse.egit.github.core.client.GitHubClient.createException(GitHubClient.java:552)
at
org.eclipse.egit.github.core.client.GitHubClient.sendJson(GitHubClient.java:643)
at
org.eclipse.egit.github.core.client.GitHubClient.post(GitHubClient.java:757)
at
org.eclipse.egit.github.core.service.DataService.createTree(DataService.java:203)
at
com.apps.ui5.accelerator.writers.GHFileWriter.addFileToGH(GHFileWriter.java:87)
at
com.apps.ui5.accelerator.writers.GHFileWriter.main(GHFileWriter.java:30)
Edit: The error causing code is commented. The commented code starts with "//Code for adding folder, not working"
Possible issue is with this line of code: folderEntry.setSha(blob_sha);
The error is thrown at this line: Tree newTree = dataService.createTree(repository, entries, baseTree.getSha());
Edit(VonC response): Hi VonC, thanks a lot for your response.
JGit looks promising but i see below issues.
I am running this as a service in a cloud instance where i dont know if i can rely on filerepo which is refererred in the cookbook example. If this works in cloud, i can consider that as a solution.
The example is also adding a simple file but not a folder/directory.
And I need to push changes to GitHub using a java program, not git commands. How can i do that?
Can you refer any example which is creating a folder? I tried below code for adding a folder but did not work!
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import org.dstadler.jgit.helper.CookbookHelper;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.NoFilepatternException;
import org.eclipse.jgit.lib.Repository;
public class GHFolderWriter {
public static void main(String[] args) throws IOException, NoFilepatternException, GitAPIException {
// prepare a new test-repository
try (Repository repository = CookbookHelper.createNewRepository()) {
try (Git git = new Git(repository)) {
//create folder
File folder = new File(repository.getDirectory().getParent(), "folder1");
if(!folder.createNewFile()) {
throw new IOException("Could not create folder " + folder);
}
// create the file
File myfile = new File(folder, "testfile1.txt");
if(!myfile.createNewFile()) {
throw new IOException("Could not create file " + myfile);
}
// Stage all files in the repo including new files
git.add().addFilepattern(".").call();
// and then commit the changes.
git.commit()
.setMessage("Commit all changes including additions")
.call();
try(PrintWriter writer = new PrintWriter(myfile)) {
writer.append("Hello, world!");
}
// Stage all changed files, omitting new files, and commit with one command
git.commit()
.setAll(true)
.setMessage("Commit changes to all files")
.call();
System.out.println("Committed all changes to repository at " + repository.getDirectory());
}
}
}
}
It is throwing this error:
Exception in thread "main" java.io.IOException: Could not open file.
Reason : The system cannot find the path specified (path
C:\Users\ramgood\AppData\Local\Temp\TestGitRepository441020326444846897\folder1\testfile1.txt,
working dir C:\Users\ramgood\workspace-sapui5-1.32\accelerate) at
java.io.WinNTFileSystem.createFileExclusively(Native Method) at
java.io.File.createNewFile(File.java:1016) at
com.apps.ui5.accelerator.writers.GHFolderWriter.main(GHFolderWriter.java:37)
Edit(3): I moved to jgit as you suggested and below is my new program. I am able to 1. clone remote repo with my local repo 2. add a folder and file to local repo and commit locally but 3. Failing to push to github.
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
/**
* Note: This snippet is not done and likely does not show anything useful yet
*
* #author dominik.stadler at gmx.at
*/
public class PushToRemoteRepository {
private static final String REMOTE_URL = "https://github.com/debsap/testrepo.git";
public static void main(String[] args) throws IOException, GitAPIException {
// prepare a new folder for the cloned repository
File localPath = File.createTempFile("REStGitRepository", "");
if(!localPath.delete()) {
throw new IOException("Could not delete temporary file " + localPath);
}
// then clone
System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
try (Git git = Git.cloneRepository()
.setURI(REMOTE_URL)
.setCredentialsProvider(new UsernamePasswordCredentialsProvider("debsap", "testpasword1"))
.setDirectory(localPath)
.call()) {
Repository repository = git.getRepository();
// create the folder
File theDir = new File(repository.getDirectory().getParent(), "dir1");
theDir.mkdir();
// create the file
File myfile = new File(theDir, "testfile2.txt");
if(!myfile.createNewFile()) {
throw new IOException("Could not create file " + myfile);
}
// Stage all files in the repo including new files
git.add().addFilepattern(".").call();
// and then commit the changes.
git.commit().setMessage("Commit all changes including additions").call();
try(PrintWriter writer = new PrintWriter(myfile)) {
writer.append("Hello, world!");
}
// Stage all changed files, omitting new files, and commit with one command
git.commit()
.setAll(true)
.setMessage("Commit changes to all files")
.call();
// now open the created repository
FileRepositoryBuilder builder = new FileRepositoryBuilder();
try (Repository repository1 = builder.setGitDir(localPath)
.readEnvironment() // scan environment GIT_* variables
.findGitDir() // scan up the file system tree
.build()) {
try (Git git1 = new Git(repository1)) {
git1.push().call();
}
System.out.println("Pushed from repository: " + repository1.getDirectory() + " to remote repository at " + REMOTE_URL);
}
}
}
}
I am getting below error while trying to push to remote repo or github.
Cloning from https://github.com/debsap/testrepo.git to
C:\Users\ramgood\AppData\Local\Temp\REStGitRepository8321744366017013430
Exception in thread "main"
org.eclipse.jgit.api.errors.TransportException: origin: not found. at
org.eclipse.jgit.api.PushCommand.call(PushCommand.java:183) at
org.dstadler.jgit.unfinished.PushToRemoteRepository.main(PushToRemoteRepository.java:88)
Caused by: org.eclipse.jgit.errors.NoRemoteRepositoryException:
origin: not found. at
org.eclipse.jgit.transport.TransportLocal$1.open(TransportLocal.java:131)
at
org.eclipse.jgit.transport.TransportBundleFile$1.open(TransportBundleFile.java:106)
at org.eclipse.jgit.transport.Transport.open(Transport.java:565) at
org.eclipse.jgit.transport.Transport.openAll(Transport.java:383) at
org.eclipse.jgit.api.PushCommand.call(PushCommand.java:147) ... 1
more
Below is the config file from .git folder. Note that i cannot edit this manually and should be updated through java program only.
[core] symlinks = false repositoryformatversion = 0 filemode =
false logallrefupdates = true [remote "origin"] url =
https://github.com/debsap/testrepo.git fetch =
+refs/heads/:refs/remotes/origin/ [branch "master"] remote = origin merge = refs/heads/master
Edit(Solution):
Below is the working program.
public void pushToRemote(String xmlViewContent) throws IOException, InvalidRemoteException, TransportException, GitAPIException{
// prepare a new folder for the cloned repository
File localPath = File.createTempFile("GitRepository", "");
if(!localPath.delete()) {
throw new IOException("Could not delete temporary file " + localPath);
}
// then clone
System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
try (Git git = Git.cloneRepository()
.setURI(REMOTE_URL)
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password))
.setDirectory(localPath)
.call()) {
Repository repository = git.getRepository();
// create the folder
File theDir = new File(repository.getDirectory().getParent(), "webapp");
theDir.mkdir();
// create the file
File myfile = new File(theDir, "InputView.view.xml");
myfile.createNewFile();
// Stage all files in the repo including new files
git.add().addFilepattern(".").call();
// and then commit the changes.
git.commit().setMessage("Commit all changes including additions").call();
try(PrintWriter writer = new PrintWriter(myfile)) {
writer.append( xmlViewContent );
}
// Stage all changed files, omitting new files, and commit with one command
git.commit()
.setAll(true)
.setMessage("Commit changes to all files")
.call();
git.add().addFilepattern("*").call();
RevCommit result = git.commit().setMessage("initial commit").call();
git.push()
.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password))
.call();
System.out.println("Pushed with commit: "+ result);
}
}
Thanks to VonC for his responses.
For manipulating files in a Git repo, you might be better off using JGit directly, not Egit-GitHub (though it is based on JGit).
That way, you can benefit from the examples in centic9/jgit-cookbook, including a jgit/porcelain/CommitAll.java example using addFilepattern:
// Stage all files in the repo including new files
git.add().addFilepattern(".").call();
That way, you can add any folder (content) you need.
I am running this as a service in a cloud instance where I dont know if I can rely on filerepo which is refererred in the cookbook example. If this works in cloud, i can consider that as a solution.
You will rely on the same file repo you are using right now.
The example is also adding a simple file but not a folder/directory.
Simply replace the file by a folder: it will work and add the folder content.
And I need to push changes to GitHub using a java program, not git commands. How can I do that?
Simply by adding a remote to the GitHub repo, and pushing.
Both are available in JGit (so in Java).

WatchService.take() does not wait on Windows-7-x64, while watching changes in the remote directory

I am trying to watch the remote directory (on the network machine) using WatchService available in Java SE 7. It works just fine when I run the program on Windows XP, but on Windows 7 the WatchService.take() doesn't wait, returning an invalid key.
Here is the sample code:
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.StandardWatchEventKinds;
import java.nio.file.WatchEvent;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.WatchEvent.Kind;
public class MyWatchingService {
public void watchDir(Path path) throws IOException, InterruptedException {
try (WatchService watchService = FileSystems.getDefault().newWatchService()) {
path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE);
while (true) {
// gets a watch key
final WatchKey key = watchService.take();
// retrieves pending events for a key.
for (WatchEvent<?> watchEvent : key.pollEvents()) {
// retrieves the event type and count.
// gets the kind of event (create, delete)
final Kind<?> kind = watchEvent.kind();
// handles OVERFLOW event
if (kind == StandardWatchEventKinds.OVERFLOW) {
continue;
}
final WatchEvent<Path> watchEventPath = cast(watchEvent);
final Path entry = watchEventPath.context();
// outputs an event type and entry
System.out.println(kind + " -> " + entry);
}
boolean valid = key.reset();
//exit loop if the key is not valid
if (!valid) {
System.out.println("Key is invalid!");
break;
}
}
}
}
public void writeFile (Path path, String fileName) throws IOException {
Path target = Paths.get(path.toString(), fileName);
if (target.toFile().exists()) {
Files.delete(target);
System.out.println("file is deleted ok!");
}
Path fileTo = Files.createFile(target);
Files.write(fileTo, String.valueOf(true).getBytes(), StandardOpenOption.WRITE);
System.out.println("file is written ok!");
}
#SuppressWarnings("unchecked")
private static <T> WatchEvent<T> cast(WatchEvent<?> event) {
return (WatchEvent<T>)event;
}
/**
* #param args
*/
public static void main(String[] args) {
// case 1: network directory mapped to Y volume
final Path path = Paths.get("Y:/workspace");
// case 2: local directory:
// final Path path = Paths.get("D:/workspace");
MyWatchingService service = new MyWatchingService();
try {
long start = System.currentTimeMillis();
service.watchDir(path);
System.out.println("exiting after " + Long.toString(System.currentTimeMillis() - start) + "ms...");
service.writeFile(path, "test");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Here is the output:
Key is invalid!
exiting after 16ms...
file is deleted ok!
file is written ok!
As you can see, I checked whether I can write/delete file in this network directory. I also tried without mapping defining a path like this: //server/workspace with no luck either. If I ran the same program for the commented out case 2 with local directory, then it goes into waiting with no output as expected.
I use jdk1.7.0_17, 64 bit. (I tried jre7, 32 bit, but no luck either). The remote directory is located on Windows Server 2008 R2 Standard.
I read in the API the following sentence which make me suspicious that it is not possible at all:
If a watched file is not located on a local storage device then it is
implementation specific if changes to the file can be detected. In
particular, it is not required that changes to files carried out on
remote systems be detected.
It will be bad news for me as I invested a plenty of time in this strategy using WatchService. Does someone have any ideas/experience why it doesn't work on Windows 7, or is it at all possible? And if not, what would be an alternative approach.
As the docs say: I wouldn't expect 'watching' to work on a network drive at all.
If it does work or not certainly depends on the OS of the client and maybe its configuration and on the OS of the server and maybe its configuration, plus some other, unknown factors; seems extremely fragile and not worth counting on.
A simple alternative is mentioned in the docs, too:
[...] or to use a primitive mechanism, such as polling, when a native facility is not available
The polling approach can be implemented in pure Java without using NIO at all. Warning: Heavy resource-usage (network) may be the result.
And, as always, be advised to step back a little and re-consider if you really need to build your 'network protocol' upon those file-system operations, or if you can achieve what you want/need in another way.

Android/Java -- Post simple text to Facebook wall?

I'm trying to integrate posting to one's wall from within my app. I already have an area where the user can save his/her username and password (encrypted). I would like my program to recall the saved username and password, pass that to Facebook for authentication, and then allow the app to post simple text (maybe a link too) to the user's wall.
That said, I've read everything on the developer pages at Facebook (the api looks completely foreign to me... I've never done any type of web app development before... just desktop apps), and experimented with the Java libraries here but to be honest, I don't understand any of the various implementations. Some claim to be simple to use, but apparently they are all way above my head.
I've even tried messing with the official Facebook Android SDK, but that uses a webview interface, and I can't pass in the username and password for easy authentication. Plus, I'm still clueless as to how to post to the wall even after correct authentication.
Please help.
Thanks.
Oh, btw I already have a Facebook API key and Application ID.
[UPDATE 1]
For further clarification:
If I use the following code snippet with the official Facebook Android SDK http://github.com/facebook/facebook-android-sdk what should I do next (after the user has logged-in)? This is unclear to me.
Facebook facebookClient = new Facebook();
facebookClient.authorize(this, "[APP ID]", new String[] {"publish_stream", "read_stream", "offline_access"}, this);
where "this" is an Activity that implements a DialogListener, and "[APP ID]" is my Facebook application ID.
Thanks.
[UPDATE 2]
I found a solution (see below), though the only thing missing is the ability to auto-populate the login text boxes with the data I have stored in the app. The official Facebook Android SDK may not allow for this. I'll keep looking into it.
I figured it out, with Tom's help (thanks). The key was creating a dialog with the "stream.publish" API call, using the Facebook Android SDK. Here are the steps:
Download the official Facebook Android SDK : http://github.com/facebook/facebook-android-sdk
Import the project files into Eclipse.
Export the project as a *.jar file. (this might cause a conflict)
[UPDATE]
Facebook recently updated the source code and I noticed the icon file caused resource id conflicts with my projects (Android 1.5+). My solution is to forget about exporting as a jar. Instead, copy the Facebook "com" folder directly into your app's "src" folder (i.e. "com.facebook.android" should be a package in your app... right alongside your source files). If you already have a "com" folder in your "src" folder, don't worry about any dialog boxes that appear about overwriting files, none of your source files should be overwritten. Go back into Eclipse, and refresh the "src" folder and "com.facebook.android" should now be listed as a package. Copy one of the included Facebook icons to your app's "drawable" folder and refresh that as well. Eclipse will complain about the "FbDialog.java" file... just add an import pointing to your app's "R" file to the header of that file (e.g. if your app's package name is "com.android.myapp," then add this: "import com.android.myapp.R;"). Go to #5 if you needed to do this.
Add the .jar file to your project's build path
Look at the following simplified example code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import com.facebook.android.*;
import com.facebook.android.Facebook.DialogListener;
public class FacebookActivity extends Activity implements DialogListener,
OnClickListener
{
private Facebook facebookClient;
private LinearLayout facebookButton;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.test);//my layout xml
facebookButton = (LinearLayout)this.findViewById(R.id.Test_Facebook_Layout);
}
#Override
public void onComplete(Bundle values)
{
if (values.isEmpty())
{
//"skip" clicked ?
return;
}
// if facebookClient.authorize(...) was successful, this runs
// this also runs after successful post
// after posting, "post_id" is added to the values bundle
// I use that to differentiate between a call from
// faceBook.authorize(...) and a call from a successful post
// is there a better way of doing this?
if (!values.containsKey("post_id"))
{
try
{
Bundle parameters = new Bundle();
parameters.putString("message", "this is a test");// the message to post to the wall
facebookClient.dialog(this, "stream.publish", parameters, this);// "stream.publish" is an API call
}
catch (Exception e)
{
// TODO: handle exception
System.out.println(e.getMessage());
}
}
}
#Override
public void onError(DialogError e)
{
System.out.println("Error: " + e.getMessage());
}
#Override
public void onFacebookError(FacebookError e)
{
System.out.println("Error: " + e.getMessage());
}
#Override
public void onCancel()
{
}
#Override
public void onClick(View v)
{
if (v == facebookButton)
{
facebookClient = new Facebook();
// replace APP_API_ID with your own
facebookClient.authorize(this, APP_API_ID,
new String[] {"publish_stream", "read_stream", "offline_access"}, this);
}
}
}
AsyncFacebookRunner mAsyncRunner;
Facebook facebook =new Facebook("Your app id");
btnLogin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
facebook.authorize(FbdemoActivity.this, new String[]{ "user_photos,publish_checkins,publish_actions,publish_stream"},new DialogListener() {
#Override
public void onComplete(Bundle values) {
}
#Override
public void onFacebookError(FacebookError error) {
}
#Override
public void onError(DialogError e) {
}
#Override
public void onCancel() {
}
});
}
});
public void postOnWall(String msg) {
Log.d("Tests", "Testing graph API wall post");
try {
String response = facebook.request("me");
Bundle parameters = new Bundle();
parameters.putString("message", msg);
parameters.putString("description", "test test test");
response = facebook.request("me/feed", parameters,
"POST");
Log.d("Tests", "got response: " + response);
if (response == null || response.equals("") ||
response.equals("false")) {
Log.v("Error", "Blank response");
}
} catch(Exception e) {
e.printStackTrace();
}
Here is an objective answer to your new question, "What do I do next?"
A quick look at the source code leads me to believe this is what you do:
Check this URL for the REST (http://en.wikipedia.org/wiki/Representational_State_Transfer) API methods you can use to leave a comment/post:
http://developers.facebook.com/docs/reference/rest/
Specifically this: http://developers.facebook.com/docs/reference/rest/links.post
Check out lines 171 through 295 of Facebook.java
http://github.com/facebook/facebook-android-sdk/blob/master/facebook/src/com/facebook/android/Facebook.java
To see how to use the API to make these requests.
You'll probably want this method (it's overloaded, see the code).
/**
* Make a request to Facebook's old (pre-graph) API with the given
* parameters. One of the parameter keys must be "method" and its value
* should be a valid REST server API method.
*
* See http://developers.facebook.com/docs/reference/rest/
*
* Note that this method blocks waiting for a network response, so do not
* call it in a UI thread.
*
* Example:
* <code>
* Bundle parameters = new Bundle();
* parameters.putString("method", "auth.expireSession");
* String response = request(parameters);
* </code>
*
* #param parameters
* Key-value pairs of parameters to the request. Refer to the
* documentation: one of the parameters must be "method".
* #throws IOException
* if a network error occurs
* #throws MalformedURLException
* if accessing an invalid endpoint
* #throws IllegalArgumentException
* if one of the parameters is not "method"
* #return JSON string representation of the response
*/
public String request(Bundle parameters)
To those who have problems, in the new facebook(); , the string is you App_id, and just delete the APP_ID in the authorized call.
Don't know why the error message is shown, but I guess that facebook updated the facebook SDK.

How do I set the Eclipse build path and class path from an Ant build file?

There's a lot of discussion about Ant and Eclipse, but no previously answered seems to help me.
Here's the deal: I am trying to build a Java program that compiles successfully with Ant from the command-line. (To confuse matters further, the program I am attempting to compile is Ant itself.)
What I really want to do is to bring this project into Eclipse and have it compile in Eclipse such that the type bindings and variable bindings (nomenclature from Eclipse JDT) are correctly resolved. I need this because I need to run a static analysis on the code that is built on top of Eclipse JDT. The normal way I bring a Java project into Eclipse so that Eclipse will build it and resolve all the bindings is to just import the source directories into a Java project, and then tell it to use the src/main/ directory as a "source directory."
Unfortunately, doing that with Ant causes the build to fail with numerous compile errors. It seems to me that the Ant build file is setting up the class path and build path correctly (possibly by excluding certain source files) and Eclipse does not have this information.
Is there any way to take the class path & build path information embedded in an Ant build file, and given that information to Eclipse to put in its .project and .classpath files? I've tried, creating a new project from an existing build file (an option in the File menu) but this does not help. The project still has the same compile errors.
Thanks,
Nels
I've never found a really clean way to do it, but one "hackish" way to do it is to manipulate the .classpath file eclipse uses (this contains the build path).
So the .classpath is going to have stuff in it like this:
<classpathentry kind="lib" path="C:/jboss-4.2.3.GA/client/jboss-system-client.jar"/>
So you could, for example, write some sort of batch script, etc. which would read your ant file dependencies and put them into the eclipse .classpath file (in the proper format, of course).
But personally, I never fool with such things. What I do is just put all the jars my project needs in one folder, and then in my ant file I have a path set up like this:
<path id="all_libs">
<fileset dir="test_reflib">
<include name="**/*.jar"/>
</fileset>
</path>
test_reflib just needs to be defined to wherever this folder is that contains all the jars.
Then, on the eclipse side you can just do a "Add jars" and navigate to this same folder and just pick all the jars. What's even cooler is that any time you drop new jars into this folder, just click at the root level in the eclipse project and do "Refresh", and then edit the build path and click add jar again and it will only show you the jars that you haven't already added to the build path yet (i.e. the new jar you just dropped into the folder).
This obviously doesn't work too well if you are sharing jars in a central place, but it works pretty well for smaller projects where you can just copy all the jars over to a centralized folder for the project.
I use the ivy to manage my ANT classpaths, I highly recommend learning how it works.
There is an eclipse plugin that will manage the eclipse classpath from the same ivy.xml file that ANT uses to define it's dependencies.
I wrote an Ant Task that generates an Eclipse .userlibraries file. You can import the generated file to create a user library in Eclipse. And then use this user library as part of your build path.
To use the task add this to your ant build file:
<target name="createEclipseUserLibraries"
description="Creates classpath and bootclasspatch that can be imported into Eclipse">
<taskdef name="createEclipseUserLibraries"
classname="com.forumsys.tools.CreateEclipseUserLibraries"
classpathref="yourclasspathref"/>
<createEclipseUserLibraries classpathref="classpathref" bootclasspathref="bootclasspathref"/>
</target>
Ant Task. It requires ant.jar to run and compile:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Reference;
/**
* A custom tag to create a file the eclipse can import to setup a user libraries.
*
* Created: Mar 29, 2014 9:44:09 AM
*
* #author Javier S. López
* #version 1.0
*/
public class CreateEclipseUserLibraries extends Task {
public static final String UTF8_ENCODING = "UTF-8";
public static final String DEFAULT_BOOT_CLASSPATH_LIBRARY_NAME = "SYSTEM_LIBRARY";
public static final String DEFAULT_CLASSPATH_LIBRARY_NAME = "LIBRARY";
public static final String DEFAULT_DESTINATION = "Eclipse.userlibraries";
private static final String INDENT = " ";
private Path _classpath;
private Path _bootClasspath;
private String _bootClasspathLibraryName = DEFAULT_BOOT_CLASSPATH_LIBRARY_NAME;
private String _classpathLibraryName = DEFAULT_CLASSPATH_LIBRARY_NAME;
private String _destination = DEFAULT_DESTINATION;
public void setClasspath(final Path classpath) {
if (_classpath == null) {
_classpath = classpath;
} else {
_classpath.append(classpath);
}
}
public void setClasspathRef(final Reference reference) {
if (_classpath == null) {
final Project antProject = getProject();
_classpath = new Path(antProject);
}
_classpath.setRefid(reference);
}
public void setBootClasspath(final Path bootClasspath) {
if (_bootClasspath == null) {
_bootClasspath = bootClasspath;
} else {
_bootClasspath.append(bootClasspath);
}
}
public void setBootClasspathRef(final Reference reference) {
if (_bootClasspath == null) {
final Project antProject = getProject();
_bootClasspath = new Path(antProject);
}
_bootClasspath.setRefid(reference);
}
public void setClasspathLibraryName(final String name) {
if (!isEmpty(name)) {
_classpathLibraryName = name;
}
}
public void setBootClasspathLibraryName(final String name) {
if (!isEmpty(name)) {
_bootClasspathLibraryName = name;
}
}
public void setDestination(final String argDestination) {
if (!isEmpty(argDestination)) {
_destination = argDestination;
}
}
#Override
public void execute() throws BuildException {
if (_classpath == null) {
throw new BuildException("classpath or classpathref attribute must be set");
}
if (_bootClasspath == null) {
throw new BuildException("bootclasspath or bootclasspathref attribute must be set");
}
try {
createUserLibrariesFile();
} catch (final IOException e) {
throw new BuildException(e.getMessage(), e);
}
}
/**
* #throws IOException
*
*/
private void createUserLibrariesFile() throws IOException {
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<?final xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>");
stringBuilder.append("\n");
stringBuilder.append("<eclipse-userlibraries version=\"2\">").append("\n");
createBootClasspathLibrary(stringBuilder);
createClasspathLibrary(stringBuilder);
stringBuilder.append("</eclipse-userlibraries>");
final Project antProject = getProject();
final File baseDir = antProject.getBaseDir();
final File file = new File(baseDir, _destination);
if (file.exists()) {
file.delete();
}
final boolean append = false;
BufferedOutputStream bos = null;
try {
final FileOutputStream fos = new FileOutputStream(file, append);
bos = new BufferedOutputStream(fos);
bos.write(stringBuilder.toString().getBytes(UTF8_ENCODING));
bos.flush();
} finally {
if (bos != null) {
bos.close();
}
}
}
/**
* #param stringBuilder
*
*/
private void createBootClasspathLibrary(final StringBuilder stringBuilder) {
createLibrary(stringBuilder, _bootClasspathLibraryName, true, _bootClasspath);
}
/**
* #param stringBuilder
*/
private void createClasspathLibrary(final StringBuilder stringBuilder) {
createLibrary(stringBuilder, _classpathLibraryName, false, _classpath);
}
/**
* #param stringBuilder
* #param bootClasspathLibraryName
* #param b
* #param bootClasspath
*/
private void createLibrary(final StringBuilder stringBuilder, final String libraryName,
final boolean isSystemLibrary, final Path path) {
stringBuilder.append(INDENT).append("<library name=\"").append(libraryName);
stringBuilder.append("\" systemlibrary=\"").append(Boolean.toString(isSystemLibrary)).append("\">\n");
final String[] paths = path.list();
final Project antProject = getProject();
final File baseDir = antProject.getBaseDir();
final String baseDirName = baseDir.getName();
for (final String strPath : paths) {
final int index = strPath.indexOf(baseDirName);
//Only include the relative path
if (index != -1) {
stringBuilder.append(INDENT).append(INDENT);
stringBuilder.append("<archive path=\"").append(
strPath.substring(index - 1)).append("\"/>\n");
}
}
stringBuilder.append(INDENT).append("</library>\n");
}
public static final boolean isEmpty(final String str) {
return (str == null) || (str.length() == 0);
}
}
From the raw ant distribution, first run "ant -f fetch.xml" (or similar) to download a lot of needed dependencies. Add these to your Eclipse project and see if it helps.
We have generated Eclipse .classpath and .project files from Ant for a large project with centrally located jars (100+) (not counting src jars and javadocs). Similar to the build.xml linked from here with the obvious addition of the src and javadoc attributes.

Basic File upload in GWT

I'm trying to figure out how to upload one file using GWTs FileUpload widget. I'm using GWT and Google AppEngine with Java but I would like to upload file to my own Linux server.
I have the following code already but now I can't figure out how to submit my file to the Google AppServer server and save it to another server:
public class FileUploader{
private ControlPanel cp;
private FormPanel form = new FormPanel();
private FileUpload fu = new FileUpload();
public FileUploader(ControlPanel cp) {
this.cp = cp;
this.cp.setPrimaryArea(getFileUploaderWidget());
}
#SuppressWarnings("deprecation")
public Widget getFileUploaderWidget() {
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
// form.setAction(/* WHAT SHOULD I PUT HERE */);
VerticalPanel holder = new VerticalPanel();
fu.setName("upload");
holder.add(fu);
holder.add(new Button("Submit", new ClickHandler() {
public void onClick(ClickEvent event) {
GWT.log("You selected: " + fu.getFilename(), null);
form.submit();
}
}));
form.addSubmitHandler(new FormPanel.SubmitHandler() {
public void onSubmit(SubmitEvent event) {
if (!"".equalsIgnoreCase(fu.getFilename())) {
GWT.log("UPLOADING FILE????", null);
// NOW WHAT????
}
else{
event.cancel(); // cancel the event
}
}
});
form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
public void onSubmitComplete(SubmitCompleteEvent event) {
Window.alert(event.getResults());
}
});
form.add(holder);
return form;
}
}
Now, what do I need to do next? What do i need to put in web.xml and how do I write my servlet so i can store file and return url of that object (if possible)
Here's the code from my app:
1) I created a class to accept http request:
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class FileUpload extends HttpServlet{
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletFileUpload upload = new ServletFileUpload();
try{
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
// Process the input stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[8192];
while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
}
int maxFileSize = 10*(1024*1024); //10 megs max
if (out.size() > maxFileSize) {
throw new RuntimeException("File is > than " + maxFileSize);
}
}
}
catch(Exception e){
throw new RuntimeException(e);
}
}
}
2) Then in my web.xml I've added these lines:
<servlet>
<servlet-name>fileUploaderServlet</servlet-name>
<servlet-class>com.testapp.server.FileUpload</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>fileUploaderServlet</servlet-name>
<url-pattern>/testapp/fileupload</url-pattern>
</servlet-mapping>
3) And for form.action did this:
form.setAction(GWT.getModuleBaseURL()+"fileupload");
I would suggest using GWTUpload because it's dead simple to use and extend. You can add it to your project in less than 10 minutes and it supports GAE right out of the box (using GWTUpload-GAE). See the examples for some common usage scenarios.
In GWT, you can post the file to the server using http form methods, and you must use the supplied HttpServlet to accept and save the data as binary blogs in the Appengine BigTable.
Then, you need a second HttpServlet to read the file from bigtable, SET THE MIME TYPE IN THE HTTP HEADER {and caching options}, and then stream the file to the user.
Although RPC isn't NECESSARILY needed, you must let the client know what the generated fileId is so they can access it {unless you want to let user's supply the id and force them to worry about name overrides... ...ick}. Either you can use rpc to ask for a list of / single id {like "newest file id by user"}, or you can return that id in the body of the UploadServlet's response... but then you must make sure your post target is an in-page iframe, poll to make sure the iframe has a body between the submit event and the actual server response, and then parse and use that id in gwt to create an img or object tag that uses the file.
The key part is having one servlet for upload, and another to download. Remember, BigTable just stores binary blobs, so you also need your data entity to have a mime/content Type that can be read from the input file {never rely on file extensions!}. Also, there's a 1MB per entity in the BigTable, and a 10MB request limit for free accounts. You may wish to have your data entity contain a list of 1-10 blobs, each of which are a max 1024bytes.
Basically, your best bet is to find a working, free copy, like Google File Service, and extend it to learn how the system works.
If you wish, I will be posting my own open-source version of file handling, once I finish the gwt control widgets and can consider it all stable enough to be useful to anyone. Email x AT aiyx DOT info if you want me to send you a jar of betalicious code.
Here you go with a complete GWT fileupload with Progress bar
Here you can DOWNLOAD the source

Categories