/*
===========================================================================
Copyright (c) 2013 3PillarGlobal
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sub-license, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===========================================================================
*/
package org.brickred.tools;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.brickred.socialauth.util.AccessGrant;
import org.codehaus.jackson.map.ObjectMapper;
/**
* Generates {@link AccessGrant} object from a given file which is generated by
* {@link GenerateToken} through command line utility.
*
* @author tarun.nagpal
*
*/
public class Importer {
private static final Log LOG = LogFactory.getLog(Importer.class);
/**
* Generates AccessGrant object from a file generated by
* {@link GenerateToken}
*
* @param reader
* Reader object created with access token file generated by
* {@link GenerateToken}
* @return AccessGrant object
* @throws Exception
*/
public static AccessGrant importAccessGrant(final Reader reader)
throws Exception {
LOG.info("Importing AccessGrant");
StringBuffer sb = new StringBuffer();
char[] buf = new char[1024];
int numRead = 0;
while ((numRead = reader.read(buf)) != -1) {
String readData = String.valueOf(buf, 0, numRead);
sb.append(readData);
buf = new char[1024];
}
reader.close();
String jsonStr = sb.toString();
LOG.debug("AccessGrant json :: " + jsonStr);
ObjectMapper mapper = new ObjectMapper();
AccessGrant grant = mapper.readValue(jsonStr, AccessGrant.class);
LOG.debug(grant.toString());
return grant;
}
/**
* Generates AccessGrant object from a file generated by
* {@link GenerateToken}
*
* @param filePath
* path of file generated by {@link GenerateToken}
* @return AccessGrant object
* @throws Exception
*/
public static AccessGrant importAccessGrant(final String filePath)
throws Exception {
LOG.info("Importing AccessGrant from :: " + filePath);
File file = new File(filePath);
FileReader reader = new FileReader(file);
return importAccessGrant(reader);
}
}