In my program, I am trying to get the last modified date for some items in my box folders, but I am unable to access that information.

According to the documentation for the box API and the javadoc for the java library, any of the following methods of BoxTypedObject should return the information I want:

  • getModifiedAt() should return a String in ISO 8601 format for the date.
  • getModifiedDate() should return a Date object for the date.
  • getValue("modified_at") should also return a String in ISO 8601 format for the date.
  • getExtraData("modified_at") is also a possible way, although I am not as sure of this one.

However, none of those methods has worked for me; they all just returh null.

The (vastly simplified) code that I am using to retrieve the dates is as follows, with one of the above methods substituted for the comment block:

private static void printAll(BoxFolder boxFolder){
    for(BoxTypedObject file : boxFolder.getItemCollection().getEntries())
        System.out.printf("[\"%1$s\" %2$s]%n",
                file.getValue("name"), file./*[???]*/);
}

The other fields all return the correct values, only when I try to get the date does it fail on me.

How do I retrieve the modified date for the BoxTypedObjects?

EDIT: I have figured out one way to get it, but it is somewhat slow.

client.getFilesManager().getFile(file.getId(), null).getModifiedAt()

retrieves the date. I am still interested if there is a better way to do it, though.


Additional information (may or may not be relevant to the problem):

The authentication of the box client is handled by the following class:

import java.awt.Desktop;
import java.io.*;
import java.net.*;
import com.box.boxjavalibv2.BoxClient;
import com.box.boxjavalibv2.dao.BoxOAuthToken;
import com.box.boxjavalibv2.exceptions.*;
import com.box.boxjavalibv2.requests.requestobjects.BoxOAuthRequestObject;
import com.box.restclientv2.exceptions.BoxRestException;

/**
 * This class handles the storage and use of authentication keys, to
 * simplify the process of obtaining n authenticated client.  This class
 * will store refresh keys in a file, so that it can authenticate a client
 * without needing for user intervention.
 * <p>
 * Copyright 2013 Mallick Mechanical, Inc.
 * 
 * @author Anson Mansfield
 */
public class Authenticator {

    /**
     * Constructs an {@code Authenticator} for use obtaining
     * authenticated {@Code BoxClient}s
     * 
     * @param key The OAuth client id and application key.
     * @param secret The OAuth client secret.
     * @param authFile The file to be used for storing authentications
     * for later use.
     */
    public Authenticator(String key, String secret, File authFile){
        this.key = key;
        this.secret = secret;
        this.authFile = authFile;
    }

    /**
     * Constructs a new {@Code BoxClient} object, authenticates it,
     * and returns it.
     */
    public BoxClient getAuthenticatedClient(){
        BoxClient client = new BoxClient(key,secret);
        client.authenticate(getToken(client));
        return client;
    }
    public final String host = "http://localhost";
    public final int port = 4000;
    public final String key, secret;
    public final File authFile;
    public final String url = "https://www.box.com/api/oauth2/authorize?response_type=code&client_id=";

    /**
     * Obtains a token that can be used to authenticate the box client,
     * and stores its refresh value in a file, so it can be used later.
     * @param client The client to obtain a token for.
     * @return A token that can be used to authenticate the client, or
     * {@code null} if one could not be obtained.
     */
    private BoxOAuthToken getToken(BoxClient client){
        BoxOAuthToken token = null;
        try{
            if((token = getOldToken(client)) != null) return token;
            if((token = getNewToken(client)) != null) return token;
            return token;
        }finally{
            writeNewToken(token);
        }
    }

    /**
     * Attempts to write a token's refresh token to a file.
     * @param token The token whose refresh value is to be written.
     */
    private void writeNewToken(BoxOAuthToken token) {
        if(token != null)
            try(BufferedWriter out = new BufferedWriter(new FileWriter(authFile))){
                out.write(token.getRefreshToken());
            }catch(IOException ex){
                System.out.println("couldn't update new token");
            }
    }

    /**
     * Reads the last session's refresh token from a file and attempts
     * to get a new authentication token with it.
     * @param client The client for which the authentication token is for.
     * @return The token obtained from the refresh, or {@code null} if one
     * could not be obtained.
     */
    private BoxOAuthToken getOldToken(BoxClient client) {
        System.out.println("attempting to use old token");
        BoxOAuthToken token = null;
        try(BufferedReader in = new BufferedReader(new FileReader(authFile))){
            token = client.getOAuthManager().refreshOAuth(
                    BoxOAuthRequestObject.refreshOAuthRequestObject(
                            in.readLine(), key, secret
                            ));
            System.out.println("refreshed old token");
        }catch(IOException ex){
            System.out.println("couldn't read old token");
        } catch(BoxRestException | BoxServerException | AuthFatalFailureException ex){
            System.out.println("couldn't refresh old token");
        }
        return token;
    }

    /**
     * Connects to the OAuth server and gets a new authentication token.
     * @param client The client to get a token for.
     * @return The new token obtained from the server, or {@code null} if one could not be obtained.
     */
    private BoxOAuthToken getNewToken(BoxClient client) {
        System.out.println("attempting to get new token");
        BoxOAuthToken token = null;
        try {
            Desktop.getDesktop().browse(java.net.URI.create(url + key));
            token = client.getOAuthManager().createOAuth(
                    BoxOAuthRequestObject.createOAuthRequestObject(getCode(), key, secret, host + port)
                    );
        } catch (BoxRestException | BoxServerException |  AuthFatalFailureException | IOException ex) {
            ex.printStackTrace();
            return null;
        }
        return token;
    }

    /**
     * This listens on the configured port for the code included in the callback.
     * It also deploys a script on the receiving socket to close the browser tab navigating to it.
     * @return The authentication code to generate a token with.
     */
    private String getCode(){
        try (ServerSocket serverSocket = new ServerSocket(port);
                Socket socket = serverSocket.accept();
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                PrintWriter out = new PrintWriter(socket.getOutputStream());){
            out.println("<script type=\"text/javascript\">");
            out.println(    "window.open('', '_self', '');");
            out.println(    "window.close();");
            out.println("</script>"); //Close the tab
            while (true){
                String code = "";
                code = in.readLine ();
                System.out.println(code);
                String match = "code";
                int loc = code.indexOf(match);
                if( loc >0 ) {
                    int httpstr = code.indexOf("HTTP")-1;
                    code = code.substring(code.indexOf(match), httpstr);
                    String parts[] = code.split("=");
                    code=parts[1];
                    return code;
                } else {
                    // It doesn't have a code
                }
            }
        } catch (IOException | NullPointerException e) {
            return "";
        }
    }
}

The actual class that will be getting the modified field (not yet finished, though):

import java.io.File;
import java.util.Scanner;
import com.box.boxjavalibv2.BoxClient;
import com.box.boxjavalibv2.dao.BoxFolder;
import com.box.boxjavalibv2.dao.BoxTypedObject;

/**
 * Copyright 2013 Mallick Mechanical, Inc.
 * 
 * @author Anson Mansfield
 */
public class BoxStuff {
    static BoxClient client;
    public void main(String ... args) throws Exception {
        client = new Authenticator(args[0], args[1], new File(args[2]))
        .getAuthenticatedClient();
        userSelectFolder("Select the project folder");
    }
    private static BoxFolder userSelectFolder(String prompt) throws Exception{
        Scanner kbd;
        if(System.console()!=null)
            kbd = new Scanner(System.console().reader());
        else
            kbd = new Scanner(System.in);

        String line = "";

        System.out.println();
        System.out.println(prompt);
        System.out.println("(leave prompt blank to select folder)");
        BoxFolder current = client.getFoldersManager().getFolder("0", null);
        printAll(current);
        System.out.print("select>");
        while(!(line = kbd.nextLine()).isEmpty()){
            BoxFolder next = select(current, Integer.parseInt(line));
            if(next != null) current = next;
            printAll(current);
            System.out.print("select>");
        }
        return current;
    }
    private static void printAll(BoxFolder boxFolder){
        int idx=0;
        System.out.println("  0:[parent folder]");
        for(BoxTypedObject file : boxFolder
                .getItemCollection()
                .getEntries()){
            if(file.getType().equals("folder")){
                System.out.printf("%1$3d:[%2$-32s %3$-6s %4$-9s]%n",
                        ++idx, format((String) file.getValue("name"),30), file.getType(), file.getId());
            } else {
                System.out.printf("    [%1$-32s %2$-6s %3$-9s Edit:%4$s]%n",
                        format((String) file.getValue("name"),32), file.getType(), file.getId(), file.getExtraData("modified_at"));
            }
        }
    }
    private static String format(CharSequence source, int length){
        StringBuilder b = new StringBuilder(length);

        b.append('"');

        if(source.length() > 30)
            b.append(source.subSequence(0, 29)).append('~');
        else
            b.append(String.format("%1$-30s",source));
        b.append('"');
        return b.toString();
    }
    private static BoxFolder select(BoxFolder boxFolder, int i) throws Exception{
        int idx=0;
        for(BoxTypedObject file : boxFolder.getItemCollection().getEntries()){
            if(file.getType().equals("folder") && ++idx == i){
                return client.getFoldersManager().getFolder(file.getId(), null);
            }
        }

        if(idx==0){
            if(boxFolder.getParent() == null)
                return client.getFoldersManager().getFolder("0", null);
            else
                return client.getFoldersManager().getFolder(boxFolder.getParent().getId(), null);
        }else{
            System.out.println("Selection is out of range!");
            return boxFolder;
        }
    }
}

If anyone else wants to use these classes for something, just ask me. It is probably OK (they are a mechanical contractor, not a software company), I just need to clear it with my boss (This code does still belong to the company).

有帮助吗?

解决方案

This is actually a little tricky. The api call to get folder items by default only return children items with some default fields, they don't include fields like modified_at. However if you supply extra fields parameters you should be able to get them.

Here is what you can do when using the getFolderItems method(this is also in the readme in github): BoxFolderRequestObject requestObj = BoxFolderRequestObject.getFolderItemsRequestObject(30, 20) .addField(BoxFolder.FIELD_NAME) .addField(BoxFolder.FIELD_MODIFIED_AT); BoxCollection collection = boxClient.getFoldersManager().getFolderItems(folderId, requestObj);

There is another tricky thing here though, after you supply these fields, the result children items will only contain the supplied fields(plus some basic fields), so make sure you add all the fields you want.

其他提示

Here is one way I figured out to get it (that actually works):

client.getFilesManager().getFile(file.getId(), null).getModifiedAt()

This, however, is somewhat slow, so I would greatly appreciate it if someone else knows a faster solution.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top