Example usage for com.fasterxml.jackson.databind ObjectMapper readValue

List of usage examples for com.fasterxml.jackson.databind ObjectMapper readValue

Introduction

In this page you can find the example usage for com.fasterxml.jackson.databind ObjectMapper readValue.

Prototype

@SuppressWarnings("unchecked")
    public <T> T readValue(byte[] src, JavaType valueType)
            throws IOException, JsonParseException, JsonMappingException 

Source Link

Usage

From source file:com.twentyn.patentScorer.ScoreMerger.java

public static void main(String[] args) throws Exception {
    System.out.println("Starting up...");
    System.out.flush();/*w  w w .j av  a 2  s  . com*/
    Options opts = new Options();
    opts.addOption(Option.builder("h").longOpt("help").desc("Print this help message and exit").build());

    opts.addOption(Option.builder("r").longOpt("results").required().hasArg()
            .desc("A directory of search results to read").build());
    opts.addOption(Option.builder("s").longOpt("scores").required().hasArg()
            .desc("A directory of patent classification scores to read").build());
    opts.addOption(Option.builder("o").longOpt("output").required().hasArg()
            .desc("The output file where results will be written.").build());

    HelpFormatter helpFormatter = new HelpFormatter();
    CommandLineParser cmdLineParser = new DefaultParser();
    CommandLine cmdLine = null;
    try {
        cmdLine = cmdLineParser.parse(opts, args);
    } catch (ParseException e) {
        System.out.println("Caught exception when parsing command line: " + e.getMessage());
        helpFormatter.printHelp("DocumentIndexer", opts);
        System.exit(1);
    }

    if (cmdLine.hasOption("help")) {
        helpFormatter.printHelp("DocumentIndexer", opts);
        System.exit(0);
    }
    File scoresDirectory = new File(cmdLine.getOptionValue("scores"));
    if (cmdLine.getOptionValue("scores") == null || !scoresDirectory.isDirectory()) {
        LOGGER.error("Not a directory of score files: " + cmdLine.getOptionValue("scores"));
    }

    File resultsDirectory = new File(cmdLine.getOptionValue("results"));
    if (cmdLine.getOptionValue("results") == null || !resultsDirectory.isDirectory()) {
        LOGGER.error("Not a directory of results files: " + cmdLine.getOptionValue("results"));
    }

    FileWriter outputWriter = new FileWriter(cmdLine.getOptionValue("output"));

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);

    FilenameFilter jsonFilter = new FilenameFilter() {
        public final Pattern JSON_PATTERN = Pattern.compile("\\.json$");

        public boolean accept(File dir, String name) {
            return JSON_PATTERN.matcher(name).find();
        }
    };

    Map<String, PatentScorer.ClassificationResult> scores = new HashMap<>();
    LOGGER.info("Reading scores from directory at " + scoresDirectory.getAbsolutePath());
    for (File scoreFile : scoresDirectory.listFiles(jsonFilter)) {
        BufferedReader reader = new BufferedReader(new FileReader(scoreFile));
        int count = 0;
        String line;
        while ((line = reader.readLine()) != null) {
            PatentScorer.ClassificationResult res = objectMapper.readValue(line,
                    PatentScorer.ClassificationResult.class);
            scores.put(res.docId, res);
            count++;
        }
        LOGGER.info("Read " + count + " scores from " + scoreFile.getAbsolutePath());
    }

    Map<String, List<DocumentSearch.SearchResult>> synonymsToResults = new HashMap<>();
    Map<String, List<DocumentSearch.SearchResult>> inchisToResults = new HashMap<>();
    LOGGER.info("Reading results from directory at " + resultsDirectory);
    // With help from http://stackoverflow.com/questions/6846244/jackson-and-generic-type-reference.
    JavaType resultsType = objectMapper.getTypeFactory().constructCollectionType(List.class,
            DocumentSearch.SearchResult.class);

    List<File> resultsFiles = Arrays.asList(resultsDirectory.listFiles(jsonFilter));
    Collections.sort(resultsFiles, new Comparator<File>() {
        @Override
        public int compare(File o1, File o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    for (File resultsFile : resultsFiles) {
        BufferedReader reader = new BufferedReader(new FileReader(resultsFile));
        CharBuffer buffer = CharBuffer.allocate(Long.valueOf(resultsFile.length()).intValue());
        int bytesRead = reader.read(buffer);
        LOGGER.info("Read " + bytesRead + " bytes from " + resultsFile.getName() + " (length is "
                + resultsFile.length() + ")");
        List<DocumentSearch.SearchResult> results = objectMapper.readValue(new CharArrayReader(buffer.array()),
                resultsType);

        LOGGER.info("Read " + results.size() + " results from " + resultsFile.getAbsolutePath());

        int count = 0;
        for (DocumentSearch.SearchResult sres : results) {
            for (DocumentSearch.ResultDocument resDoc : sres.getResults()) {
                String docId = resDoc.getDocId();
                PatentScorer.ClassificationResult classificationResult = scores.get(docId);
                if (classificationResult == null) {
                    LOGGER.warn("No classification result found for " + docId);
                } else {
                    resDoc.setClassifierScore(classificationResult.getScore());
                }
            }
            if (!synonymsToResults.containsKey(sres.getSynonym())) {
                synonymsToResults.put(sres.getSynonym(), new ArrayList<DocumentSearch.SearchResult>());
            }
            synonymsToResults.get(sres.getSynonym()).add(sres);
            count++;
            if (count % 1000 == 0) {
                LOGGER.info("Processed " + count + " search result documents");
            }
        }
    }

    Comparator<DocumentSearch.ResultDocument> resultDocumentComparator = new Comparator<DocumentSearch.ResultDocument>() {
        @Override
        public int compare(DocumentSearch.ResultDocument o1, DocumentSearch.ResultDocument o2) {
            int cmp = o2.getClassifierScore().compareTo(o1.getClassifierScore());
            if (cmp != 0) {
                return cmp;
            }
            cmp = o2.getScore().compareTo(o1.getScore());
            return cmp;
        }
    };

    for (Map.Entry<String, List<DocumentSearch.SearchResult>> entry : synonymsToResults.entrySet()) {
        DocumentSearch.SearchResult newSearchRes = null;
        // Merge all result documents into a single search result.
        for (DocumentSearch.SearchResult sr : entry.getValue()) {
            if (newSearchRes == null) {
                newSearchRes = sr;
            } else {
                newSearchRes.getResults().addAll(sr.getResults());
            }
        }
        if (newSearchRes == null || newSearchRes.getResults() == null) {
            LOGGER.error("Search results for " + entry.getKey() + " are null.");
            continue;
        }
        Collections.sort(newSearchRes.getResults(), resultDocumentComparator);
        if (!inchisToResults.containsKey(newSearchRes.getInchi())) {
            inchisToResults.put(newSearchRes.getInchi(), new ArrayList<DocumentSearch.SearchResult>());
        }
        inchisToResults.get(newSearchRes.getInchi()).add(newSearchRes);
    }

    List<String> sortedKeys = new ArrayList<String>(inchisToResults.keySet());
    Collections.sort(sortedKeys);
    List<GroupedInchiResults> orderedResults = new ArrayList<>(sortedKeys.size());
    Comparator<DocumentSearch.SearchResult> synonymSorter = new Comparator<DocumentSearch.SearchResult>() {
        @Override
        public int compare(DocumentSearch.SearchResult o1, DocumentSearch.SearchResult o2) {
            return o1.getSynonym().compareTo(o2.getSynonym());
        }
    };
    for (String inchi : sortedKeys) {
        List<DocumentSearch.SearchResult> res = inchisToResults.get(inchi);
        Collections.sort(res, synonymSorter);
        orderedResults.add(new GroupedInchiResults(inchi, res));
    }

    objectMapper.writerWithView(Object.class).writeValue(outputWriter, orderedResults);
    outputWriter.close();
}

From source file:HelloSmartsheet.java

public static void main(String[] args) {
    HttpURLConnection connection = null;
    StringBuilder response = new StringBuilder();

    //We are using Jackson JSON parser to deserialize the JSON. See http://wiki.fasterxml.com/JacksonHome
    //Feel free to use which ever library you prefer.
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

    try {/*from   w  w  w. j av  a 2  s .  c o m*/

        System.out.println("STARTING HelloSmartsheet...");
        //Create a BufferedReader to read user input.
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter Smartsheet API access token:");
        String accessToken = in.readLine();
        System.out.println("Fetching list of your sheets...");
        //Create a connection and fetch the list of sheets
        connection = (HttpURLConnection) new URL(GET_SHEETS_URL).openConnection();
        connection.addRequestProperty("Authorization", "Bearer " + accessToken);
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        //Read the response line by line.
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }
        reader.close();
        //Use Jackson to conver the JSON string to a List of Sheets
        List<Sheet> sheets = mapper.readValue(response.toString(), new TypeReference<List<Sheet>>() {
        });
        if (sheets.size() == 0) {
            System.out.println("You don't have any sheets.  Goodbye!");
            return;
        }
        System.out.println("Total sheets: " + sheets.size());
        int i = 1;
        for (Sheet sheet : sheets) {
            System.out.println(i++ + ": " + sheet.name);
        }
        System.out.print("Enter the number of the sheet you want to share: ");

        //Prompt the user to provide the sheet number, the email address, and the access level
        Integer sheetNumber = Integer.parseInt(in.readLine().trim()); //NOTE: for simplicity, error handling and input validation is neglected.
        Sheet chosenSheet = sheets.get(sheetNumber - 1);

        System.out.print("Enter an email address to share " + chosenSheet.getName() + " to: ");
        String email = in.readLine();

        System.out.print("Choose an access level (VIEWER, EDITOR, EDITOR_SHARE, ADMIN) for " + email + ": ");
        String accessLevel = in.readLine();

        //Create a share object
        Share share = new Share();
        share.setEmail(email);
        share.setAccessLevel(accessLevel);

        System.out.println("Sharing " + chosenSheet.name + " to " + email + " as " + accessLevel + ".");

        //Create a connection. Note the SHARE_SHEET_URL uses /sheet as opposed to /sheets (with an 's')
        connection = (HttpURLConnection) new URL(SHARE_SHEET_URL.replace(SHEET_ID, "" + chosenSheet.getId()))
                .openConnection();
        connection.setDoOutput(true);
        connection.addRequestProperty("Authorization", "Bearer " + accessToken);
        connection.addRequestProperty("Content-Type", "application/json");

        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
        //Serialize the Share object
        writer.write(mapper.writeValueAsString(share));
        writer.close();

        //Read the response and parse the JSON
        reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        response = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            response.append(line);
        }

        Result result = mapper.readValue(response.toString(), Result.class);
        System.out.println("Sheet shared successfully, share ID " + result.result.id);
        System.out.println("Press any key to quit.");
        in.read();

    } catch (IOException e) {
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(((HttpURLConnection) connection).getErrorStream()));
        String line;
        try {
            response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
            Result result = mapper.readValue(response.toString(), Result.class);
            System.out.println(result.message);
        } catch (IOException e1) {
            e1.printStackTrace();
        }

    } catch (Exception e) {
        System.out.println("Something broke: " + e.getMessage());
        e.printStackTrace();
    }

}

From source file:edu.usf.cutr.siri.SiriParserJacksonExample.java

/**
 * Takes in a path to a JSON or XML file, parses the contents into a Siri object, 
 * and prints out the contents of the Siri object.
 * //www  .  j  a  v a2s .  c  o  m
 * @param args path to the JSON or XML file located on disk
 */
public static void main(String[] args) {

    if (args[0] == null) {
        System.out.println("Proper Usage is: java JacksonSiriParserExample path-to-siri-file-to-parse");
        System.exit(0);
    }

    try {

        //Siri object we're going to instantiate based on JSON or XML data
        Siri siri = null;

        //Get example JSON or XML from file
        File file = new File(args[0]);

        System.out.println("Input file = " + file.getAbsolutePath());

        /*
         * Alternately, instead of passing in a File, a String encoded in JSON or XML can be
         * passed into Jackson for parsing.  Uncomment the below line to read the 
         * JSON or XML into the String.
         */
        //String inputExample = readFile(file);   

        String extension = FilenameUtils.getExtension(args[0]);

        if (extension.equalsIgnoreCase("json")) {
            System.out.println("Parsing JSON...");
            ObjectMapper mapper = null;

            try {
                mapper = (ObjectMapper) SiriUtils.readFromCache(SiriUtils.OBJECT_MAPPER);
            } catch (Exception e) {
                System.out.println("Error reading from cache: " + e);
            }

            if (mapper == null) {
                // instantiate ObjectMapper like normal if cache read failed
                mapper = new ObjectMapper();

                //Jackson 2.0 configuration settings
                mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
                mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
                mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
                mapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
                mapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);

                //Tell Jackson to expect the JSON in PascalCase, instead of camelCase
                mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.PascalCaseStrategy());
            }

            //Deserialize the JSON from the file into the Siri object
            siri = mapper.readValue(file, Siri.class);

            /*
             * Alternately, you can also deserialize the JSON from a String into the Siri object.
             * Uncomment the below line to parsing the JSON from a String instead of the File.
             */
            //siri = mapper.readValue(inputExample, Siri.class);

            //Write the ObjectMapper to the cache, to speed up parsing for the next execution
            SiriUtils.forceCacheWrite(mapper);

        }

        if (extension.equalsIgnoreCase("xml")) {
            System.out.println("Parsing XML...");
            //Use Aalto StAX implementation explicitly            
            XmlFactory f = new XmlFactory(new InputFactoryImpl(), new OutputFactoryImpl());

            JacksonXmlModule module = new JacksonXmlModule();

            /**
             * Tell Jackson that Lists are using "unwrapped" style (i.e., 
             * there is no wrapper element for list). This fixes the error
             * "com.fasterxml.jackson.databind.JsonMappingException: Can not
             * >> instantiate value of type [simple type, class >>
             * uk.org.siri.siri.VehicleMonitoringDelivery] from JSON String;
             * no >> single-String constructor/factory method (through
             * reference chain: >>
             * uk.org.siri.siri.Siri["ServiceDelivery"]->
             * uk.org.siri.siri.ServiceDel >>
             * ivery["VehicleMonitoringDelivery"])"
             * 
             * NOTE - This requires Jackson v2.1
             */
            module.setDefaultUseWrapper(false);

            /**
             * Handles "xml:lang" attribute, which is used in SIRI
             * NaturalLanguage String, and looks like: <Description
             * xml:lang="EN">b/d 1:00pm until f/n. loc al and express buses
             * run w/delays & detours. POTUS visit in MANH. Allow additional
             * travel time Details at www.mta.info</Description>
             * 
             * Passing "Value" (to match expected name in XML to map,
             * considering naming strategy) will make things work. This is
             * since JAXB uses pseudo-property name of "value" for XML Text
             * segments, whereas Jackson by default uses "" (to avoid name
             * collisions).
             * 
             * NOTE - This requires Jackson v2.1
             * 
             * NOTE - This still requires a CustomPascalCaseStrategy to
             * work. Please see the CustomPascalCaseStrategy in this app
             * that is used below.
             */
            module.setXMLTextElementName("Value");

            XmlMapper xmlMapper = null;

            try {
                xmlMapper = (XmlMapper) SiriUtils.readFromCache(SiriUtils.XML_MAPPER);
            } catch (Exception e) {
                System.out.println("Error reading from cache: " + e);
            }

            if (xmlMapper == null) {
                xmlMapper = new XmlMapper(f, module);

                xmlMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
                xmlMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
                xmlMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY, true);
                xmlMapper.configure(DeserializationFeature.READ_ENUMS_USING_TO_STRING, true);

                /**
                 * Tell Jackson to expect the XML in PascalCase, instead of camelCase
                 * NOTE:  We need the CustomPascalStrategy here to handle XML 
                 * namespace attributes such as xml:lang.  See the comments in 
                 * CustomPascalStrategy for details.
                 */
                xmlMapper.setPropertyNamingStrategy(new CustomPascalCaseStrategy());
            }

            //Parse the SIRI XML response            
            siri = xmlMapper.readValue(file, Siri.class);

            //Write the XmlMapper to the cache, to speed up parsing for the next execution
            SiriUtils.forceCacheWrite(xmlMapper);
        }

        //If we successfully retrieved and parsed JSON or XML, print the contents
        if (siri != null) {
            SiriUtils.printContents(siri);
        }

    } catch (IOException e) {
        System.err.println("Error reading or parsing input file: " + e);
    }

}

From source file:AdminExample.java

public static void main(String[] args) {
    HttpURLConnection connection = null;
    StringBuilder response = new StringBuilder();

    //We are using Jackson JSON parser to serialize and deserialize the JSON. See http://wiki.fasterxml.com/JacksonHome
    //Feel free to use which ever library you prefer.
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    String accessToken = "";//Insert your access token here. Note this must be from an account that is an Admin of an account.
    String user1Email = ""; //You need access to these two email account.
    String user2Email = ""; //Note Gmail and Hotmail allow email aliasing. 
    //joe@gmail.com will get email sent to joe+user1@gmail.com 

    try {/* w w  w.  j  a v  a  2s. c  om*/
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Adding user " + user1Email);

        //Add the users:
        User user = new User();
        user.setEmail(user1Email);
        user.setAdmin(false);
        user.setLicensedSheetCreator(true);

        connection = (HttpURLConnection) new URL(USERS_URL).openConnection();
        connection.addRequestProperty("Authorization", "Bearer " + accessToken);
        connection.addRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);
        mapper.writeValue(connection.getOutputStream(), user);
        Result<User> newUser1Result = mapper.readValue(connection.getInputStream(),
                new TypeReference<Result<User>>() {
                });
        System.out.println(
                "User " + newUser1Result.result.email + " added with userId " + newUser1Result.result.getId());

        user = new User();
        user.setEmail(user2Email);
        user.setAdmin(true);
        user.setLicensedSheetCreator(true);

        connection = (HttpURLConnection) new URL(USERS_URL).openConnection();
        connection.addRequestProperty("Authorization", "Bearer " + accessToken);
        connection.addRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);
        mapper.writeValue(connection.getOutputStream(), user);
        Result<User> newUser2Result = mapper.readValue(connection.getInputStream(),
                new TypeReference<Result<User>>() {
                });
        System.out.println(
                "User " + newUser2Result.result.email + " added with userId " + newUser2Result.result.getId());
        System.out.println("Please visit the email inbox for the users " + user1Email + " and  " + user2Email
                + " and confirm membership to the account.");
        System.out.print("Press Enter to continue");
        in.readLine();

        //List all the users of the org
        connection = (HttpURLConnection) new URL(USERS_URL).openConnection();
        connection.addRequestProperty("Authorization", "Bearer " + accessToken);
        connection.addRequestProperty("Content-Type", "application/json");
        List<User> users = mapper.readValue(connection.getInputStream(), new TypeReference<List<User>>() {
        });

        System.out.println("The following are members of your account: ");

        for (User orgUser : users) {
            System.out.println("\t" + orgUser.getEmail());
        }

        //Create a sheet as the admin
        Sheet newSheet = new Sheet();
        newSheet.setName("Admin's Sheet");
        newSheet.setColumns(Arrays.asList(new Column("Column 1", "TEXT_NUMBER", null, true, null),
                new Column("Column 2", "TEXT_NUMBER", null, null, null),
                new Column("Column 3", "TEXT_NUMBER", null, null, null)));
        connection = (HttpURLConnection) new URL(SHEETS_URL).openConnection();
        connection.addRequestProperty("Authorization", "Bearer " + accessToken);
        connection.addRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);
        mapper.writeValue(connection.getOutputStream(), newSheet);
        mapper.readValue(connection.getInputStream(), new TypeReference<Result<Sheet>>() {
        });

        //Create a sheet as user1
        newSheet = new Sheet();
        newSheet.setName("User 1's Sheet");
        newSheet.setColumns(Arrays.asList(new Column("Column 1", "TEXT_NUMBER", null, true, null),
                new Column("Column 2", "TEXT_NUMBER", null, null, null),
                new Column("Column 3", "TEXT_NUMBER", null, null, null)));
        connection = (HttpURLConnection) new URL(SHEETS_URL).openConnection();
        connection.addRequestProperty("Authorization", "Bearer " + accessToken);
        //Here is where the magic happens - Any action performed in this call will be on behalf of the
        //user provided. Note that this person must be a confirmed member of your org. 
        //Also note that the email address is url-encoded.
        connection.addRequestProperty("Assume-User", URLEncoder.encode(user1Email, "UTF-8"));
        connection.addRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);
        mapper.writeValue(connection.getOutputStream(), newSheet);
        mapper.readValue(connection.getInputStream(), new TypeReference<Result<Sheet>>() {
        });

        //Create a sheet as user2
        newSheet = new Sheet();
        newSheet.setName("User 2's Sheet");
        newSheet.setColumns(Arrays.asList(new Column("Column 1", "TEXT_NUMBER", null, true, null),
                new Column("Column 2", "TEXT_NUMBER", null, null, null),
                new Column("Column 3", "TEXT_NUMBER", null, null, null)));
        connection = (HttpURLConnection) new URL(SHEETS_URL).openConnection();
        connection.addRequestProperty("Authorization", "Bearer " + accessToken);
        connection.addRequestProperty("Assume-User", URLEncoder.encode(user2Email, "UTF-8"));
        connection.addRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);
        mapper.writeValue(connection.getOutputStream(), newSheet);
        mapper.readValue(connection.getInputStream(), new TypeReference<Result<Sheet>>() {
        });

        //List all the sheets in the org:
        System.out.println("The following sheets are owned by members of your account: ");
        connection = (HttpURLConnection) new URL(USERS_SHEETS_URL).openConnection();
        connection.addRequestProperty("Authorization", "Bearer " + accessToken);
        connection.addRequestProperty("Content-Type", "application/json");
        List<Sheet> allSheets = mapper.readValue(connection.getInputStream(), new TypeReference<List<Sheet>>() {
        });

        for (Sheet orgSheet : allSheets) {
            System.out.println("\t" + orgSheet.getName() + " - " + orgSheet.getOwner());
        }

        //Now delete user1 and transfer their sheets to user2
        connection = (HttpURLConnection) new URL(USER_URL.replace(ID, newUser1Result.getResult().getId() + "")
                + "?transferTo=" + newUser2Result.getResult().getId()).openConnection();
        connection.addRequestProperty("Authorization", "Bearer " + accessToken);
        connection.addRequestProperty("Assume-User", URLEncoder.encode(user2Email, "UTF-8"));
        connection.addRequestProperty("Content-Type", "application/json");
        connection.setRequestMethod("DELETE");
        Result<Object> resultObject = mapper.readValue(connection.getInputStream(),
                new TypeReference<Result<Object>>() {
                });

        System.out.println("Sheets transferred : " + resultObject.getSheetsTransferred());

    } catch (IOException e) {

        InputStream is = connection == null ? null : ((HttpURLConnection) connection).getErrorStream();
        if (is != null) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line;
            try {
                response = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();
                Result<?> result = mapper.readValue(response.toString(), Result.class);
                System.err.println(result.message);

            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        e.printStackTrace();

    } catch (Exception e) {
        System.out.println("Something broke: " + e.getMessage());
        e.printStackTrace();
    }
}

From source file:SheetStructure.java

public static void main(String[] args) {
    HttpURLConnection connection = null;
    StringBuilder response = new StringBuilder();

    //We are using Jackson JSON parser to serialize and deserialize the JSON. See http://wiki.fasterxml.com/JacksonHome
    //Feel free to use which ever library you prefer.
    ObjectMapper mapper = new ObjectMapper();
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    String accessToken = "";//Insert your access token here.

    try {/*  ww w  . java 2 s. co  m*/

        System.out.println("Starting HelloSmartsheet2: Betty's Bake Sale...");
        //First Create a new sheet.
        String sheetName = "Betty's Bake Sale";
        //We will be using POJOs to represent the REST request objects. We will convert these to and from JSON using Jackson JSON.
        //Their structure directly relates to the JSON that gets passed through the API.
        //Note that these POJOs are included as static inner classes to keep this to one file. Normally they would be broken out.
        Sheet newSheet = new Sheet();
        newSheet.setName(sheetName);
        newSheet.setColumns(Arrays.asList(new Column("Baked Goods", "TEXT_NUMBER", null, true, null),
                new Column("Baker", "CONTACT_LIST", null, null, null),
                new Column("Price Per Item", "TEXT_NUMBER", null, null, null),
                new Column("Gluten Free?", "CHECKBOX", "FLAG", null, null), new Column("Status", "PICKLIST",
                        null, null, Arrays.asList("Started", "Finished", "Delivered"))));
        connection = (HttpURLConnection) new URL(GET_SHEETS_URL).openConnection();
        connection.addRequestProperty("Authorization", "Bearer " + accessToken);
        connection.addRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);
        mapper.writeValue(connection.getOutputStream(), newSheet);
        Result<Sheet> newSheetResult = mapper.readValue(connection.getInputStream(),
                new TypeReference<Result<Sheet>>() {
                });
        newSheet = newSheetResult.getResult();
        System.out.println("Sheet " + newSheet.getName() + " created, id: " + newSheet.getId());

        //Now add a column:
        String columnName = "Delivery Date";
        System.out.println("Adding column " + columnName + " to " + sheetName);
        Column newColumn = new Column(columnName, "DATE", 5);

        connection = (HttpURLConnection) new URL(SHEET_COLUMNS_URL.replace(ID, "" + newSheet.getId()))
                .openConnection();
        connection.addRequestProperty("Authorization", "Bearer " + accessToken);
        connection.addRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);
        mapper.writeValue(connection.getOutputStream(), newColumn);
        Result<Column> newColumnResult = mapper.readValue(connection.getInputStream(),
                new TypeReference<Result<Column>>() {
                });

        System.out.println(
                "Column " + newColumnResult.getResult().getTitle() + " added to " + newSheet.getName());

        //Next, we will get the list of Columns from the API. We could figure this out based on what the server has returned in the result, but we'll just ask the API for it.
        System.out.println("Fetching " + newSheet.getName() + " sheet columns...");
        connection = (HttpURLConnection) new URL(SHEET_COLUMNS_URL.replace(ID, "" + newSheet.getId()))
                .openConnection();
        connection.addRequestProperty("Authorization", "Bearer " + accessToken);
        List<Column> allColumns = mapper.readValue(connection.getInputStream(),
                new TypeReference<List<Column>>() {
                });
        System.out.println("Fetched.");

        //Now we will be adding rows
        System.out.println("Inserting rows into " + newSheet.getName());
        List<Row> rows = new ArrayList<Row>();
        rows.add(new Row(Arrays.asList(new Cell(allColumns.get(0).id, "Brownies"),
                new Cell(allColumns.get(1).id, "julieann@example.com"), new Cell(allColumns.get(2).id, "$1"),
                new Cell(allColumns.get(3).id, Boolean.TRUE), new Cell(allColumns.get(4).id, "Finished"))));
        rows.add(new Row(Arrays.asList(new Cell(allColumns.get(0).id, "Snickerdoodles"),
                new Cell(allColumns.get(1).id, "stevenelson@example.com"), new Cell(allColumns.get(2).id, "$1"),
                new Cell(allColumns.get(3).id, Boolean.FALSE), new Cell(allColumns.get(4).id, "Delivered"),
                new Cell(allColumns.get(5).id, "2013-09-04"))));
        rows.add(new Row(Arrays.asList(new Cell(allColumns.get(0).id, "Rice Krispy Treats"),
                new Cell(allColumns.get(1).id, "rickthames@example.com"),
                new Cell(allColumns.get(2).id, "$.50"), new Cell(allColumns.get(3).id, Boolean.TRUE),
                new Cell(allColumns.get(4).id, "Started"))));
        rows.add(new Row(Arrays.asList(new Cell(allColumns.get(0).id, "Muffins"),
                new Cell(allColumns.get(1).id, "sandrassmart@example.com"),
                new Cell(allColumns.get(2).id, "$1.50"), new Cell(allColumns.get(3).id, Boolean.FALSE),
                new Cell(allColumns.get(4).id, "Finished"))));
        rows.add(new Row(Arrays.asList(new Cell(allColumns.get(0).id, "Chocolate Chip Cookies"),
                new Cell(allColumns.get(1).id, "janedaniels@example.com"), new Cell(allColumns.get(2).id, "$1"),
                new Cell(allColumns.get(3).id, Boolean.FALSE), new Cell(allColumns.get(4).id, "Delivered"),
                new Cell(allColumns.get(5).id, "2013-09-05"))));
        rows.add(new Row(Arrays.asList(new Cell(allColumns.get(0).id, "Ginger Snaps"),
                new Cell(allColumns.get(1).id, "nedbarnes@example.com"), new Cell(allColumns.get(2).id, "$.50"),
                new Cell(allColumns.get(3).id, Boolean.TRUE),
                new Cell(allColumns.get(4).id, "Unknown", false)))); //Note that this one is strict=false. This is because "Unknown" was not one of the original options when the column was created.

        RowWrapper rowWrapper = new RowWrapper();
        rowWrapper.setToBottom(true);
        rowWrapper.setRows(rows);

        connection = (HttpURLConnection) new URL(SHEET_ROWS_URL.replace(ID, "" + newSheet.getId()))
                .openConnection();
        connection.addRequestProperty("Authorization", "Bearer " + accessToken);
        connection.addRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);
        mapper.writeValue(connection.getOutputStream(), rowWrapper);
        Result<List<Row>> newRowsResult = mapper.readValue(connection.getInputStream(),
                new TypeReference<Result<List<Row>>>() {
                });
        System.out.println("Added " + newRowsResult.getResult().size() + " rows to " + newSheet.getName());

        //Move a row to the top.
        System.out.println("Moving row 6 to the top.");
        RowWrapper moveToTop = new RowWrapper();
        moveToTop.setToTop(true);

        connection = (HttpURLConnection) new URL(
                ROW_URL.replace(ID, "" + newRowsResult.getResult().get(5).getId())).openConnection();
        connection.setRequestMethod("PUT");
        connection.addRequestProperty("Authorization", "Bearer " + accessToken);
        connection.addRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);
        mapper.writeValue(connection.getOutputStream(), moveToTop);
        mapper.readValue(connection.getInputStream(), new TypeReference<Result<List<Row>>>() {
        });

        System.out.println("Row 6 moved to top.");

        //Insert empty rows for spacing
        rows = new ArrayList<Row>();
        rows.add(new Row(Arrays.asList(new Cell(allColumns.get(0).id, ""))));
        rows.add(new Row(Arrays.asList(new Cell(allColumns.get(0).id, "Delivered"))));
        rowWrapper = new RowWrapper();
        rowWrapper.setToBottom(true);
        rowWrapper.setRows(rows);

        connection = (HttpURLConnection) new URL(SHEET_ROWS_URL.replace(ID, "" + newSheet.getId()))
                .openConnection();
        connection.addRequestProperty("Authorization", "Bearer " + accessToken);
        connection.addRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);
        mapper.writeValue(connection.getOutputStream(), rowWrapper);
        Result<List<Row>> spacerRowsResult = mapper.readValue(connection.getInputStream(),
                new TypeReference<Result<List<Row>>>() {
                });
        System.out.println("Added " + spacerRowsResult.getResult().size() + " rows to " + newSheet.getName());

        //Move Delivered rows to be children of the last spacer row.
        System.out.println("Moving delivered rows to Delivered section...");
        Long[] deliveredRowIds = new Long[] { newRowsResult.result.get(1).getId(),
                newRowsResult.result.get(4).getId() };
        RowWrapper parentRowLocation = new RowWrapper();
        parentRowLocation.setParentId(spacerRowsResult.getResult().get(1).getId());

        for (Long deliveredId : deliveredRowIds) {
            System.out.println("Moving " + deliveredId + " to Delivered.");
            connection = (HttpURLConnection) new URL(ROW_URL.replace(ID, "" + deliveredId)).openConnection();
            connection.setRequestMethod("PUT");
            connection.addRequestProperty("Authorization", "Bearer " + accessToken);
            connection.addRequestProperty("Content-Type", "application/json");
            connection.setDoOutput(true);
            mapper.writeValue(connection.getOutputStream(), parentRowLocation);
            mapper.readValue(connection.getInputStream(), new TypeReference<Result<List<Row>>>() {
            });
            System.out.println("Row id " + deliveredId + " moved.");
        }

        System.out.println("Appending additional rows to items in progress...");

        List<Row> siblingRows = new ArrayList<Row>();
        siblingRows.add(new Row(Arrays.asList(new Cell(allColumns.get(0).id, "Scones"),
                new Cell(allColumns.get(1).id, "tomlively@example.com"),
                new Cell(allColumns.get(2).id, "$1.50"), new Cell(allColumns.get(3).id, Boolean.TRUE),
                new Cell(allColumns.get(4).id, "Finished"))));
        siblingRows.add(new Row(Arrays.asList(new Cell(allColumns.get(0).id, "Lemon Bars"),
                new Cell(allColumns.get(1).id, "rickthames@example.com"), new Cell(allColumns.get(2).id, "$1"),
                new Cell(allColumns.get(3).id, Boolean.FALSE), new Cell(allColumns.get(4).id, "Started"))));
        rowWrapper = new RowWrapper();
        rowWrapper.setSiblingId(newRowsResult.getResult().get(3).getId());
        rowWrapper.setRows(siblingRows);

        connection = (HttpURLConnection) new URL(SHEET_ROWS_URL.replace(ID, "" + newSheet.getId()))
                .openConnection();
        connection.addRequestProperty("Authorization", "Bearer " + accessToken);
        connection.addRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);
        mapper.writeValue(connection.getOutputStream(), rowWrapper);
        Result<List<Row>> siblingRowsResult = mapper.readValue(connection.getInputStream(),
                new TypeReference<Result<List<Row>>>() {
                });
        System.out.println("Added " + siblingRowsResult.getResult().size() + " rows to " + newSheet.getName());

        System.out.println("Moving Status column to index 1...");
        Column statusColumn = allColumns.get(4);
        Column moveColumn = new Column();
        moveColumn.setIndex(1);
        moveColumn.setTitle(statusColumn.title);
        moveColumn.setSheetId(newSheet.getId());
        moveColumn.setType(statusColumn.getType());
        connection = (HttpURLConnection) new URL(COLUMN_URL.replace(ID, "" + statusColumn.getId()))
                .openConnection();
        connection.addRequestProperty("Authorization", "Bearer " + accessToken);
        connection.addRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);
        connection.setRequestMethod("PUT");

        mapper.writeValue(connection.getOutputStream(), moveColumn);
        Result<Column> movedColumnResult = mapper.readValue(connection.getInputStream(),
                new TypeReference<Result<Column>>() {
                });
        System.out.println("Moved column " + movedColumnResult.getResult().getId());
        System.out.println("Completed Hellosmartsheet2: Betty's Bake Sale.");
    } catch (IOException e) {
        InputStream is = ((HttpURLConnection) connection).getErrorStream();
        if (is != null) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            String line;
            try {
                response = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();
                Result<?> result = mapper.readValue(response.toString(), Result.class);
                System.err.println(result.message);

            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
        e.printStackTrace();

    } catch (Exception e) {
        System.out.println("Something broke: " + e.getMessage());
        e.printStackTrace();
    }

}

From source file:io.druid.server.sql.SQLRunner.java

public static void main(String[] args) throws Exception {

    Options options = new Options();
    options.addOption("h", "help", false, "help");
    options.addOption("v", false, "verbose");
    options.addOption("e", "host", true, "endpoint [hostname:port]");

    CommandLine cmd = new GnuParser().parse(options, args);

    if (cmd.hasOption("h")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("SQLRunner", options);
        System.exit(2);/*ww  w  .  jav a 2s  .com*/
    }

    String hostname = cmd.getOptionValue("e", "localhost:8080");
    String sql = cmd.getArgs().length > 0 ? cmd.getArgs()[0] : STATEMENT;

    ObjectMapper objectMapper = new DefaultObjectMapper();
    ObjectWriter jsonWriter = objectMapper.writerWithDefaultPrettyPrinter();

    CharStream stream = new ANTLRInputStream(sql);
    DruidSQLLexer lexer = new DruidSQLLexer(stream);
    TokenStream tokenStream = new CommonTokenStream(lexer);
    DruidSQLParser parser = new DruidSQLParser(tokenStream);
    lexer.removeErrorListeners();
    parser.removeErrorListeners();

    lexer.addErrorListener(ConsoleErrorListener.INSTANCE);
    parser.addErrorListener(ConsoleErrorListener.INSTANCE);

    try {
        DruidSQLParser.QueryContext queryContext = parser.query();
        if (parser.getNumberOfSyntaxErrors() > 0)
            throw new IllegalStateException();
        //      parser.setBuildParseTree(true);
        //      System.err.println(q.toStringTree(parser));
    } catch (Exception e) {
        String msg = e.getMessage();
        if (msg != null)
            System.err.println(e);
        System.exit(1);
    }

    final Query query;
    final TypeReference typeRef;
    boolean groupBy = false;
    if (parser.groupByDimensions.isEmpty()) {
        query = Druids.newTimeseriesQueryBuilder().dataSource(parser.getDataSource())
                .aggregators(new ArrayList<AggregatorFactory>(parser.aggregators.values()))
                .postAggregators(parser.postAggregators).intervals(parser.intervals)
                .granularity(parser.granularity).filters(parser.filter).build();

        typeRef = new TypeReference<List<Result<TimeseriesResultValue>>>() {
        };
    } else {
        query = GroupByQuery.builder().setDataSource(parser.getDataSource())
                .setAggregatorSpecs(new ArrayList<AggregatorFactory>(parser.aggregators.values()))
                .setPostAggregatorSpecs(parser.postAggregators).setInterval(parser.intervals)
                .setGranularity(parser.granularity).setDimFilter(parser.filter)
                .setDimensions(new ArrayList<DimensionSpec>(parser.groupByDimensions.values())).build();

        typeRef = new TypeReference<List<Row>>() {
        };
        groupBy = true;
    }

    String queryStr = jsonWriter.writeValueAsString(query);
    if (cmd.hasOption("v"))
        System.err.println(queryStr);

    URL url = new URL(String.format("http://%s/druid/v2/?pretty", hostname));
    final URLConnection urlConnection = url.openConnection();
    urlConnection.addRequestProperty("content-type", MediaType.APPLICATION_JSON);
    urlConnection.getOutputStream().write(StringUtils.toUtf8(queryStr));
    BufferedReader stdInput = new BufferedReader(
            new InputStreamReader(urlConnection.getInputStream(), Charsets.UTF_8));

    Object res = objectMapper.readValue(stdInput, typeRef);

    Joiner tabJoiner = Joiner.on("\t");

    if (groupBy) {
        List<Row> rows = (List<Row>) res;
        Iterable<String> dimensions = Iterables.transform(parser.groupByDimensions.values(),
                new Function<DimensionSpec, String>() {
                    @Override
                    public String apply(@Nullable DimensionSpec input) {
                        return input.getOutputName();
                    }
                });

        System.out.println(
                tabJoiner.join(Iterables.concat(Lists.newArrayList("timestamp"), dimensions, parser.fields)));
        for (final Row r : rows) {
            System.out.println(tabJoiner.join(Iterables.concat(
                    Lists.newArrayList(parser.granularity.toDateTime(r.getTimestampFromEpoch())),
                    Iterables.transform(parser.groupByDimensions.values(),
                            new Function<DimensionSpec, String>() {
                                @Override
                                public String apply(@Nullable DimensionSpec input) {
                                    return Joiner.on(",").join(r.getDimension(input.getOutputName()));
                                }
                            }),
                    Iterables.transform(parser.fields, new Function<String, Object>() {
                        @Override
                        public Object apply(@Nullable String input) {
                            return r.getFloatMetric(input);
                        }
                    }))));
        }
    } else {
        List<Result<TimeseriesResultValue>> rows = (List<Result<TimeseriesResultValue>>) res;
        System.out.println(tabJoiner.join(Iterables.concat(Lists.newArrayList("timestamp"), parser.fields)));
        for (final Result<TimeseriesResultValue> r : rows) {
            System.out.println(tabJoiner.join(Iterables.concat(Lists.newArrayList(r.getTimestamp()),
                    Lists.transform(parser.fields, new Function<String, Object>() {
                        @Override
                        public Object apply(@Nullable String input) {
                            return r.getValue().getMetric(input);
                        }
                    }))));
        }
    }

    CloseQuietly.close(stdInput);
}

From source file:edu.ucsd.crbs.cws.App.java

License:asdf

public static void main(String[] args) {
    Job.REFS_ENABLED = false;/*from   w ww .ja  v  a  2  s.  c o m*/
    Workflow.REFS_ENABLED = false;
    try {

        OptionParser parser = new OptionParser() {
            {
                accepts(UPLOAD_WF_ARG, "Add/Update Workflow").withRequiredArg().ofType(File.class)
                        .describedAs("Kepler .kar file");
                //accepts(LOAD_TEST,"creates lots of workflows and jobs");
                accepts(SYNC_WITH_CLUSTER_ARG,
                        "Submits & Synchronizes Workflow Jobs on local cluster with CRBS Workflow Webservice.  Requires --"
                                + PROJECT_ARG + " --" + PORTALNAME_ARG + " --" + PORTAL_URL_ARG + " --"
                                + HELP_EMAIL_ARG).withRequiredArg().ofType(String.class).describedAs("URL");
                accepts(GEN_OLD_KEPLER_XML_ARG, "Generates version 1.x kepler xml for given workflow")
                        .withRequiredArg().ofType(String.class).describedAs("wfid or .kar file");
                accepts(UPLOAD_FILE_ARG, "Registers and uploads Workspace file to REST service")
                        .withRequiredArg().ofType(File.class);
                accepts(REGISTER_FILE_ARG,
                        "Registers Workspace file to REST service (DOES NOT UPLOAD FILE TO REST SERVICE)")
                                .withRequiredArg().ofType(File.class);
                accepts(GET_WORKSPACE_FILE_INFO_ARG, "Outputs JSON of specified workspace file(s)")
                        .withRequiredArg().ofType(String.class).describedAs("workspace file id");
                accepts(GET_WORKFLOW_ARG, "Outputs JSON of specified Workflow").withRequiredArg()
                        .ofType(Long.class).describedAs("Workflow Id");
                accepts(DOWNLOAD_FILE_ARG, "Downloads Workspace file").withRequiredArg().ofType(String.class)
                        .describedAs("workspace file id");
                accepts(UPDATE_PATH_ARG, "Updates Workspace file path").withRequiredArg().ofType(String.class)
                        .describedAs("workspace file id");
                accepts(PATH_ARG,
                        "Sets WorkspaceFile file path.  Used in coordination with --" + UPDATE_PATH_ARG)
                                .withRequiredArg().ofType(String.class).describedAs("file path");
                accepts(URL_ARG,
                        "URL to use with --" + UPLOAD_WF_ARG + ", --" + UPLOAD_FILE_ARG + ", --"
                                + GET_WORKSPACE_FILE_INFO_ARG + " flags").withRequiredArg().ofType(String.class)
                                        .describedAs("URL");
                accepts(EXAMPLE_JSON_ARG,
                        "Outputs example JSON of Job, User, Workflow, and WorkspaceFile objects");
                accepts(WF_EXEC_DIR_ARG, "Workflow Execution Directory").withRequiredArg().ofType(File.class)
                        .describedAs("Directory");
                accepts(WF_DIR_ARG, "Workflows Directory").withRequiredArg().ofType(File.class)
                        .describedAs("Directory");
                accepts(KEPLER_SCRIPT_ARG, "Kepler").withRequiredArg().ofType(File.class).describedAs("Script");
                accepts(QUEUE_ARG, "SGE Queue").withRequiredArg().ofType(String.class).describedAs("Queue");
                accepts(CAST_ARG, "Panfishcast binary").withRequiredArg().ofType(File.class)
                        .describedAs("panfishcast");
                accepts(STAT_ARG, "Panfishstat binary").withRequiredArg().ofType(File.class)
                        .describedAs("panfishstat");
                accepts(LOGIN_ARG, "User Login").withRequiredArg().ofType(String.class).describedAs("username");
                accepts(TOKEN_ARG, "User Token").withRequiredArg().ofType(String.class).describedAs("token");
                accepts(RUN_AS_ARG, "User to run as (for power accounts that can run as other users)")
                        .withRequiredArg().ofType(String.class).describedAs("runas");
                accepts(OWNER_ARG, "Sets owner when creating Workspace file and Workflow").withRequiredArg()
                        .ofType(String.class).describedAs("username");
                accepts(JOB_ID_ARG, "Sets source job id for Workspace file when used with --" + UPLOAD_FILE_ARG
                        + " and --" + REGISTER_FILE_ARG).withRequiredArg().ofType(Long.class)
                                .describedAs("Job Id");
                accepts(MD5_ARG,
                        "Sets md5 for Workspace file when used with --" + UPLOAD_FILE_ARG + " and --"
                                + REGISTER_FILE_ARG).withRequiredArg().ofType(String.class)
                                        .describedAs("MD5 message digest");
                accepts(SIZE_ARG,
                        "Sets size in bytes for Workspace file when used with --" + UPLOAD_FILE_ARG + " and --"
                                + REGISTER_FILE_ARG).withRequiredArg().ofType(Long.class)
                                        .describedAs("Size of file/dir in bytes");
                accepts(RESAVE_WORKSPACEFILE_ARG, "Resaves Workspace file").withRequiredArg().ofType(Long.class)
                        .describedAs("WorkspaceFile Id or -1 to resave all");
                accepts(RESAVE_JOB_ARG, "Resaves Job").withRequiredArg().ofType(Long.class)
                        .describedAs("Job Id or -1 to resave all");
                accepts(RESAVE_WORKFLOW_ARG, "Resaves Workflow").withRequiredArg().ofType(Long.class)
                        .describedAs("Workflow Id or -1 to resave all");
                accepts(PREVIEW_WORKFLOW_ARG,
                        "Preview Workflow on Web, requires --" + URL_ARG
                                + " currently it should be: http://imafish.dynamic.ucsd.edu/cws/makepreview")
                                        .withRequiredArg().ofType(File.class).describedAs("Kepler .kar file");
                accepts(DESCRIPTION_ARG, "Description for WorkspaceFile").withRequiredArg()
                        .ofType(String.class);
                accepts(TYPE_ARG, "Type of WorkspaceFile").withRequiredArg().ofType(String.class);
                accepts(NAME_ARG,
                        "Sets name for Workspace file when used with --" + UPLOAD_FILE_ARG + " and --"
                                + REGISTER_FILE_ARG).withRequiredArg().ofType(String.class)
                                        .describedAs("WorkspaceFile name");
                accepts(REGISTER_JAR_ARG, "Path to Jar to register WorkspaceFiles").withRequiredArg()
                        .ofType(File.class).describedAs("Path to this jar");
                accepts(GET_JOB_ARG, "Gets job from service in JSON format, requires --" + URL_ARG)
                        .withRequiredArg().ofType(Long.class).describedAs("Job Id");
                accepts(GET_WORKSPACE_FILE_ARG,
                        "Gets WorkspaceFile from service in JSON format, requires --" + URL_ARG)
                                .withRequiredArg().ofType(Long.class)
                                .describedAs("WorkspaceFile Id or -1 for all");
                accepts(PROJECT_ARG, "Project name ie CRBS.  Used with --" + SYNC_WITH_CLUSTER_ARG)
                        .withRequiredArg().ofType(String.class);
                accepts(PORTALNAME_ARG, "Portal name ie SLASH portal Used with --" + SYNC_WITH_CLUSTER_ARG)
                        .withRequiredArg().ofType(String.class);
                accepts(PORTAL_URL_ARG,
                        "Portal url ie http://slashsegmentation.com Used with --" + SYNC_WITH_CLUSTER_ARG)
                                .withRequiredArg().ofType(String.class);
                accepts(HELP_EMAIL_ARG, "Help and reply to email address Used with --" + SYNC_WITH_CLUSTER_ARG)
                        .withRequiredArg().ofType(String.class);
                accepts(BCC_EMAIL_ARG, "Blind Carbon copy email address Used with --" + SYNC_WITH_CLUSTER_ARG)
                        .withRequiredArg().ofType(String.class);
                accepts(WORKSPACE_FILE_FAILED_ARG,
                        "Denotes whether workspacefile failed (true) or not (false).  Used with --"
                                + UPDATE_PATH_ARG).withRequiredArg().ofType(Boolean.class)
                                        .describedAs("false = success and true = failed");
                accepts(ERROR_EMAIL_ARG,
                        "Email to receive notifications if errors are encountered.  Used with --"
                                + SYNC_WITH_CLUSTER_ARG).withRequiredArg().ofType(String.class);
                accepts(HELP_ARG).forHelp();
            }
        };

        OptionSet optionSet = null;
        try {
            optionSet = parser.parse(args);
        } catch (OptionException oe) {
            System.err.println("\nThere was an error parsing arguments: " + oe.getMessage() + "\n\n");
            parser.printHelpOn(System.err);
            System.exit(1);
        }

        if (optionSet.has(HELP_ARG) || (!optionSet.has(SYNC_WITH_CLUSTER_ARG) && !optionSet.has(UPLOAD_WF_ARG))
                && !optionSet.has(EXAMPLE_JSON_ARG) && !optionSet.has(UPLOAD_FILE_ARG)
                && !optionSet.has(GET_WORKSPACE_FILE_INFO_ARG) && !optionSet.has(UPDATE_PATH_ARG)
                && !optionSet.has(REGISTER_FILE_ARG) && !optionSet.has(RESAVE_WORKSPACEFILE_ARG)
                && !optionSet.has(RESAVE_JOB_ARG) && !optionSet.has(RESAVE_WORKFLOW_ARG)
                && !optionSet.has(PREVIEW_WORKFLOW_ARG) && !optionSet.has(GEN_OLD_KEPLER_XML_ARG)
                && !optionSet.has(GET_JOB_ARG) && !optionSet.has(GET_WORKSPACE_FILE_ARG)
                && !optionSet.has(GET_WORKFLOW_ARG)) {
            System.out.println(PROGRAM_HELP + "\n");
            parser.printHelpOn(System.out);
            System.exit(0);
        }

        if (optionSet.has(EXAMPLE_JSON_ARG)) {

            renderExampleWorkflowsAndTasksAsJson();
            System.exit(0);
        }

        if (optionSet.has(GET_JOB_ARG)) {
            failIfOptionSetMissingURLOrLoginOrToken(optionSet, "--" + GET_JOB_ARG + " flag");
            getJobAsJson(optionSet);
            System.exit(0);
        }
        if (optionSet.has(GET_WORKSPACE_FILE_ARG)) {
            failIfOptionSetMissingURLOrLoginOrToken(optionSet, "--" + GET_WORKSPACE_FILE_ARG + " flag");
            getWorkspaceFileAsJson(optionSet);
            System.exit(0);
        }
        if (optionSet.has(GET_WORKFLOW_ARG)) {
            failIfOptionSetMissingURLOrLoginOrToken(optionSet, "--" + GET_WORKFLOW_ARG + " flag");
            getWorkflowAsJson(optionSet);
            System.exit(0);
        }

        MultivaluedMapFactory multivaluedMapFactory = new MultivaluedMapFactoryImpl();

        if (optionSet.has(GEN_OLD_KEPLER_XML_ARG)) {
            String workflowFileOrId = (String) optionSet.valueOf(GEN_OLD_KEPLER_XML_ARG);
            File workflowFile = new File(workflowFileOrId);
            Workflow w = null;

            //if value is a file attempt to load it as a workflow file
            if (workflowFile.exists() && workflowFile.isFile()) {
                w = getWorkflowFromFile(workflowFile);
                if (w == null) {
                    throw new Exception("Unable to extract workflow from file: " + workflowFile);
                }
            } else {
                //assume the value is a workflow id and get it from the service
                //but fail if url is missing
                failIfOptionSetMissingURLOrLoginOrToken(optionSet, "--" + GEN_OLD_KEPLER_XML_ARG + " flag");
                User u = getUserFromOptionSet(optionSet);
                WorkflowRestDAOImpl workflowDAO = new WorkflowRestDAOImpl();
                workflowDAO.setRestURL((String) optionSet.valueOf(URL_ARG));
                workflowDAO.setUser(u);
                w = workflowDAO.getWorkflowById(workflowFileOrId, u);
                if (w == null) {
                    throw new Exception("Unable to extract workflow from id: " + workflowFileOrId);
                }
            }

            VersionOneWorkflowXmlWriter xmlWriter = new VersionOneWorkflowXmlWriter();
            StringWriter sw = new StringWriter();
            xmlWriter.write(sw, w);
            System.out.println(sw.toString());
            System.exit(0);

        }

        if (optionSet.has(PREVIEW_WORKFLOW_ARG)) {
            failIfOptionSetMissingURL(optionSet, "--" + PREVIEW_WORKFLOW_ARG + " flag");

            File workflowFile = (File) optionSet.valueOf(PREVIEW_WORKFLOW_ARG);
            Workflow w = getWorkflowFromFile(workflowFile);
            if (w == null) {
                throw new Exception("Unable to extract workflow from file");
            }
            uploadPreviewWorkflowFile((String) optionSet.valueOf(URL_ARG), w);
            System.exit(0);
        }

        if (optionSet.has(REGISTER_FILE_ARG)) {
            addNewWorkspaceFile(optionSet, false, REGISTER_FILE_ARG);
            System.exit(0);
        }

        if (optionSet.has(RESAVE_WORKSPACEFILE_ARG)) {
            failIfOptionSetMissingURLOrLoginOrToken(optionSet, "--" + RESAVE_WORKSPACEFILE_ARG + " flag");
            WorkspaceFileRestDAOImpl workspaceFileDAO = new WorkspaceFileRestDAOImpl();
            User u = getUserFromOptionSet(optionSet);
            workspaceFileDAO.setUser(u);
            workspaceFileDAO.setRestURL((String) optionSet.valueOf(URL_ARG));
            Long workspaceId = (Long) optionSet.valueOf(RESAVE_WORKSPACEFILE_ARG);
            if (workspaceId == -1) {
                System.out.println("Resaving all workspace files");
                List<WorkspaceFile> wsfList = workspaceFileDAO.getWorkspaceFiles(null, null, null, null, null);
                if (wsfList != null) {
                    System.out.println("Found " + wsfList.size() + " workspace files to resave");
                    for (WorkspaceFile wsf : wsfList) {
                        System.out.println("WorkspaceFile Id: " + wsf.getId());
                        workspaceFileDAO.resave(wsf.getId());
                    }
                }
            } else {
                workspaceFileDAO.resave(workspaceId);
            }
            System.exit(0);
        }

        if (optionSet.has(RESAVE_JOB_ARG)) {
            failIfOptionSetMissingURLOrLoginOrToken(optionSet, "--" + RESAVE_JOB_ARG + " flag");
            JobRestDAOImpl jobDAO = new JobRestDAOImpl();
            User u = getUserFromOptionSet(optionSet);
            jobDAO.setUser(u);
            jobDAO.setRestURL((String) optionSet.valueOf(URL_ARG));
            Long jobId = (Long) optionSet.valueOf(RESAVE_JOB_ARG);
            if (jobId == -1) {
                System.out.println("Resaving all jobs");
                List<Job> jobList = jobDAO.getJobs(null, null, null, true, true, Boolean.TRUE);
                if (jobList != null) {
                    System.out.println("Found " + jobList.size() + " jobs to resave");
                    for (Job j : jobList) {
                        System.out.println("job id: " + j.getId());
                        jobDAO.resave(j.getId());
                    }
                }
            } else {
                jobDAO.resave(jobId);
            }
            System.exit(0);
        }

        if (optionSet.has(RESAVE_WORKFLOW_ARG)) {
            failIfOptionSetMissingURLOrLoginOrToken(optionSet, "--" + RESAVE_WORKFLOW_ARG + " flag");
            WorkflowRestDAOImpl workflowDAO = new WorkflowRestDAOImpl();
            User u = getUserFromOptionSet(optionSet);
            workflowDAO.setUser(u);
            workflowDAO.setRestURL((String) optionSet.valueOf(URL_ARG));
            Long workflowId = (Long) optionSet.valueOf(RESAVE_WORKFLOW_ARG);
            if (workflowId == -1) {
                System.out.println("Resaving all workflows");
                List<Workflow> workflowList = workflowDAO.getAllWorkflows(true, Boolean.TRUE);

                if (workflowList != null) {
                    System.out.println("Found " + workflowList.size() + " workflow(s) to resave");
                    for (Workflow w : workflowList) {
                        System.out.println("workflow id: " + w.getId());
                        workflowDAO.resave(w.getId());
                    }
                }
            } else {
                workflowDAO.resave(workflowId);
            }
            System.exit(0);
        }

        if (optionSet.has(UPDATE_PATH_ARG)) {
            failIfOptionSetMissingURLOrLoginOrToken(optionSet, "--" + UPDATE_PATH_ARG + " flag");

            User u = getUserFromOptionSet(optionSet);
            String workspaceId = (String) optionSet.valueOf(UPDATE_PATH_ARG);
            String path = null;
            if (optionSet.has(PATH_ARG)) {
                path = (String) optionSet.valueOf(PATH_ARG);
            }

            String size = null;
            if (optionSet.has(SIZE_ARG)) {
                size = ((Long) optionSet.valueOf(SIZE_ARG)).toString();
            }

            if (optionSet.has(MD5_ARG)) {
                //wsp.setMd5((String)optionSet.valueOf(MD5_ARG));
            }
            Boolean isFailed = null;

            if (optionSet.has(WORKSPACE_FILE_FAILED_ARG)) {
                isFailed = (Boolean) optionSet.valueOf(WORKSPACE_FILE_FAILED_ARG);
            }

            WorkspaceFileRestDAOImpl workspaceFileDAO = new WorkspaceFileRestDAOImpl();
            workspaceFileDAO.setUser(u);
            workspaceFileDAO.setRestURL((String) optionSet.valueOf(URL_ARG));
            workspaceFileDAO.updatePathSizeAndFailStatus(Long.parseLong(workspaceId), path, size, isFailed);

            System.exit(0);
        }

        if (optionSet.has(SYNC_WITH_CLUSTER_ARG)) {
            // @TODO NEED TO MAKE JOPT DO THIS REQUIRED FLAG CHECKING STUFF
            if (!optionSet.has(WF_EXEC_DIR_ARG)) {
                System.err.println(
                        "-" + WF_EXEC_DIR_ARG + " is required with -" + SYNC_WITH_CLUSTER_ARG + " flag");
                System.exit(2);
            }
            if (!optionSet.has(WF_DIR_ARG)) {
                System.err.println("-" + WF_DIR_ARG + " is required with -" + SYNC_WITH_CLUSTER_ARG + " flag");
                System.exit(3);
            }
            if (!optionSet.has(KEPLER_SCRIPT_ARG)) {
                System.err.println(
                        "-" + KEPLER_SCRIPT_ARG + " is required with -" + SYNC_WITH_CLUSTER_ARG + " flag");
                System.exit(4);
            }

            if (!optionSet.has(CAST_ARG)) {
                System.err.println("-" + CAST_ARG + " is required with -" + SYNC_WITH_CLUSTER_ARG + " flag");
                System.exit(5);
            }

            if (!optionSet.has(STAT_ARG)) {
                System.err.println("-" + STAT_ARG + " is required with -" + SYNC_WITH_CLUSTER_ARG + " flag");
                System.exit(6);
            }

            if (!optionSet.has(QUEUE_ARG)) {
                System.err.println("-" + QUEUE_ARG + " is required with -" + SYNC_WITH_CLUSTER_ARG + " flag");
                System.exit(7);
            }

            if (!optionSet.has(REGISTER_JAR_ARG)) {
                System.err.println(
                        "-" + REGISTER_JAR_ARG + " is required with -" + SYNC_WITH_CLUSTER_ARG + " flag");
                System.exit(8);
            }

            failIfOptionSetMissingLoginOrToken(optionSet, "--" + SYNC_WITH_CLUSTER_ARG + " flag");

            File castFile = (File) optionSet.valueOf(CAST_ARG);
            String castPath = castFile.getAbsolutePath();

            File statFile = (File) optionSet.valueOf(STAT_ARG);
            String statPath = statFile.getAbsolutePath();

            String queue = (String) optionSet.valueOf(QUEUE_ARG);

            File wfExecDir = (File) optionSet.valueOf(WF_EXEC_DIR_ARG);
            File wfDir = (File) optionSet.valueOf(WF_DIR_ARG);
            File keplerScript = (File) optionSet.valueOf(KEPLER_SCRIPT_ARG);

            String registerJar = null;
            if (optionSet.has(REGISTER_JAR_ARG)) {
                File registerJarFile = (File) optionSet.valueOf(REGISTER_JAR_ARG);
                registerJar = registerJarFile.getAbsolutePath();
            }
            JobEmailNotificationData emailNotifyData = getJobEmailNotificationData(optionSet);

            User u = getUserFromOptionSet(optionSet);

            ObjectifyService.ofy();
            String url = (String) optionSet.valueOf(SYNC_WITH_CLUSTER_ARG);
            JobRestDAOImpl jobDAO = new JobRestDAOImpl();
            jobDAO.setRestURL(url);
            jobDAO.setUser(u);

            System.out.println("Running sync with cluster");

            WorkspaceFileRestDAOImpl workspaceFileDAO = new WorkspaceFileRestDAOImpl();
            workspaceFileDAO.setRestURL(url);
            workspaceFileDAO.setUser(u);
            JobPath jobPath = new JobPathImpl(wfExecDir.getAbsolutePath());
            WorkspaceFilePathSetterImpl pathSetter = new WorkspaceFilePathSetterImpl(workspaceFileDAO);

            // Submit jobs to scheduler
            JobSubmissionManager submitter = new JobSubmissionManager(jobDAO, workspaceFileDAO, pathSetter,
                    jobPath, wfDir.getAbsolutePath(), keplerScript.getAbsolutePath(), castPath, queue, u, url,
                    registerJar, emailNotifyData);

            submitter.submitJobs();

            // Update job status for all jobs in system
            MapOfJobStatusFactoryImpl jobStatusFactory = new MapOfJobStatusFactoryImpl(statPath);

            WorkflowFailedParser workflowFailedParser = new WorkflowFailedParserImpl();
            JobStatusUpdater updater = new JobStatusUpdater(jobDAO, jobStatusFactory, workflowFailedParser,
                    jobPath);
            updater.updateJobs();

            System.exit(0);
        }

        if (optionSet.has(App.GET_WORKSPACE_FILE_INFO_ARG)) {
            failIfOptionSetMissingURLOrLoginOrToken(optionSet, "--" + GET_WORKSPACE_FILE_INFO_ARG + " flag");

            WorkspaceFileRestDAOImpl workspaceFileDAO = new WorkspaceFileRestDAOImpl();

            workspaceFileDAO.setRestURL((String) optionSet.valueOf(URL_ARG));

            List<WorkspaceFile> wsFiles = workspaceFileDAO
                    .getWorkspaceFilesById((String) optionSet.valueOf(GET_WORKSPACE_FILE_INFO_ARG), null);

            if (wsFiles != null) {
                ObjectMapper om = new ObjectMapper();
                ObjectWriter ow = om.writerWithDefaultPrettyPrinter();
                System.out.print("[");
                boolean first = true;
                for (WorkspaceFile wsf : wsFiles) {
                    if (first == false) {
                        System.out.println(",");
                    } else {
                        first = false;
                    }
                    System.out.print(ow.writeValueAsString(wsf));
                }
                System.out.println("]");
            } else {
                System.err.println("[]");
            }
            System.exit(0);
        }

        if (optionSet.has(UPLOAD_FILE_ARG)) {
            addNewWorkspaceFile(optionSet, true, UPLOAD_FILE_ARG);
            System.exit(0);
        }

        if (optionSet.has(UPLOAD_WF_ARG)) {

            Long parentWfId = null;

            String postURL = null;
            if (optionSet.has(URL_ARG)) {
                postURL = (String) optionSet.valueOf(URL_ARG);
                failIfOptionSetMissingLoginOrToken(optionSet,
                        "--" + UPLOAD_WF_ARG + " and --" + URL_ARG + " flag");
            }

            File workflowFile = (File) optionSet.valueOf(UPLOAD_WF_ARG);
            Workflow w = getWorkflowFromFile(workflowFile);
            if (w != null) {

                if (optionSet.has(OWNER_ARG)) {
                    w.setOwner((String) optionSet.valueOf(OWNER_ARG));
                }

                ObjectMapper om = new ObjectMapper();
                if (parentWfId != null) {
                    w.setId(parentWfId);
                }
                if (postURL == null) {
                    System.out.println("\n--- JSON Representation of Workflow ---");
                    ObjectWriter ow = om.writerWithDefaultPrettyPrinter();
                    System.out.println(ow.writeValueAsString(w));
                    System.out.flush();
                    System.out.println("---------------------------------------");

                } else {
                    postURL = new StringBuilder().append(postURL).append(Constants.SLASH)
                            .append(Constants.REST_PATH).append(Constants.SLASH)
                            .append(Constants.WORKFLOWS_PATH).toString();

                    ClientConfig cc = new DefaultClientConfig();
                    cc.getClasses().add(StringProvider.class);
                    cc.getClasses().add(MultiPartWriter.class);
                    Client client = Client.create(cc);

                    client.setFollowRedirects(true);
                    WebResource resource = client.resource(postURL);
                    String workflowAsJson = om.writeValueAsString(w);

                    User u = getUserFromOptionSet(optionSet);
                    client.addFilter(new HTTPBasicAuthFilter(u.getLogin(), u.getToken()));
                    MultivaluedMap queryParams = multivaluedMapFactory.getMultivaluedMap(u);

                    String response = resource.queryParams(queryParams).type(MediaType.APPLICATION_JSON_TYPE)
                            .entity(workflowAsJson).post(String.class);
                    Workflow workflowRes = om.readValue(response, Workflow.class);
                    ObjectWriter ow = om.writerWithDefaultPrettyPrinter();

                    if (workflowRes.getWorkflowFileUploadURL() == null) {
                        throw new Exception(
                                "No upload url found for workflow!!!" + ow.writeValueAsString(workflowRes));
                    }

                    uploadWorkflowFile(workflowRes, workflowFile);
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        System.err.println("Caught Exception: " + ex.getMessage());

        System.exit(2);
    }

    System.exit(0);
}

From source file:com.github.arven.sleuth.commands.json.java

public static Object invoke(Interpreter env, CallStack callstack, String s) throws Exception {
    ObjectMapper m = new ObjectMapper();
    return m.readValue(s, Object.class);
}

From source file:com.github.arven.sleuth.commands.json.java

public static Object json(String s) throws Exception {
    ObjectMapper m = new ObjectMapper();
    return m.readValue(s, Object.class);
}

From source file:org.pathirage.freshet.utils.ExpressionSerde.java

public static Expression deserialize(String expression) throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();

    return objectMapper.readValue(expression, Expression.class);
}