use of org.apache.http.NameValuePair in project incubator-rya by apache.
the class SmartUriAdapter method convertUriToTypeMap.
private static Map<RyaURI, String> convertUriToTypeMap(final URI typeMapUri) throws SmartUriException {
final Map<RyaURI, String> map = new HashMap<>();
java.net.URI uri;
try {
final URIBuilder uriBuilder = new URIBuilder(typeMapUri.stringValue());
uri = uriBuilder.build();
} catch (final URISyntaxException e) {
throw new SmartUriException("Unable to parse Rya type map in Smart URI", e);
}
final List<NameValuePair> params = URLEncodedUtils.parse(uri, Charsets.UTF_8.name());
for (final NameValuePair param : params) {
final String name = param.getName();
final String value = param.getValue();
final RyaURI type = new RyaURI(name);
map.put(type, value);
}
return map;
}
use of org.apache.http.NameValuePair in project incubator-rya by apache.
the class SmartUriAdapter method createTypePropertiesUri.
private static URI createTypePropertiesUri(final ImmutableMap<RyaURI, ImmutableMap<RyaURI, Property>> typeProperties) throws SmartUriException {
final List<NameValuePair> nameValuePairs = new ArrayList<>();
for (final Entry<RyaURI, ImmutableMap<RyaURI, Property>> typeProperty : typeProperties.entrySet()) {
final RyaURI type = typeProperty.getKey();
final Map<RyaURI, Property> propertyMap = typeProperty.getValue();
final URI typeUri = createIndividualTypeWithPropertiesUri(type, propertyMap);
final String keyString = type.getDataType().getLocalName();
final String valueString = typeUri.getLocalName();
nameValuePairs.add(new BasicNameValuePair(keyString, valueString));
}
final URIBuilder uriBuilder = new URIBuilder();
uriBuilder.addParameters(nameValuePairs);
String uriString;
try {
final java.net.URI uri = uriBuilder.build();
final String queryString = uri.getRawSchemeSpecificPart();
uriString = "urn:test" + queryString;
} catch (final URISyntaxException e) {
throw new SmartUriException("Unable to create type properties for the Smart URI", e);
}
return new URIImpl(uriString);
}
use of org.apache.http.NameValuePair in project incubator-rya by apache.
the class SmartUriAdapter method createIndividualTypeWithPropertiesUri.
private static URI createIndividualTypeWithPropertiesUri(final RyaURI type, final Map<RyaURI, Property> map) throws SmartUriException {
final List<NameValuePair> nameValuePairs = new ArrayList<>();
for (final Entry<RyaURI, Property> entry : map.entrySet()) {
final RyaURI key = entry.getKey();
final Property property = entry.getValue();
final RyaType ryaType = property.getValue();
final String keyString = (new URIImpl(key.getData())).getLocalName();
final Value value = RyaToRdfConversions.convertValue(ryaType);
final String valueString = value.stringValue();
nameValuePairs.add(new BasicNameValuePair(keyString, valueString));
}
final URIBuilder uriBuilder = new URIBuilder();
uriBuilder.addParameters(nameValuePairs);
String uriString;
try {
final java.net.URI uri = uriBuilder.build();
final String queryString = uri.getRawSchemeSpecificPart();
uriString = type.getData() + /*new URIImpl(type.getData()).getLocalName()*/
queryString;
} catch (final URISyntaxException e) {
throw new SmartUriException("Unable to create type URI with all its properties for the Smart URI", e);
}
return new URIImpl(uriString);
}
use of org.apache.http.NameValuePair in project latke by b3log.
the class LatkeClient method main.
/**
* Main entry.
*
* @param args the specified command line arguments
* @throws Exception exception
*/
public static void main(String[] args) throws Exception {
// Backup Test:
// args = new String[] {
// "-h", "-backup", "-repository_names", "-verbose", "-s", "chevo2xs.appspot.com:80", "-u", "zane", "-p", "xxx", "-backup_dir",
// "C:/b3log_backup", "-w", "true"};
args = new String[] { "-h", "-restore", "-create_tables", "-verbose", "-s", "localhost:8080", "-u", "zane", "-p", "xxx", "-backup_dir", "C:/b3log_backup_devapi" };
final Options options = getOptions();
final CommandLineParser parser = new PosixParser();
try {
final CommandLine cmd = parser.parse(options, args);
serverAddress = cmd.getOptionValue("s");
backupDir = new File(cmd.getOptionValue("backup_dir"));
if (!backupDir.exists()) {
backupDir.mkdir();
}
userName = cmd.getOptionValue("u");
if (cmd.hasOption("verbose")) {
verbose = true;
}
password = cmd.getOptionValue("p");
if (verbose) {
System.out.println("Requesting server[" + serverAddress + "]");
}
final HttpClient httpClient = new DefaultHttpClient();
if (cmd.hasOption("repository_names")) {
getRepositoryNames();
}
if (cmd.hasOption("create_tables")) {
final List<NameValuePair> qparams = new ArrayList<NameValuePair>();
qparams.add(new BasicNameValuePair("userName", userName));
qparams.add(new BasicNameValuePair("password", password));
final URI uri = URIUtils.createURI("http", serverAddress, -1, CREATE_TABLES, URLEncodedUtils.format(qparams, "UTF-8"), null);
final HttpPut request = new HttpPut();
request.setURI(uri);
if (verbose) {
System.out.println("Starting create tables");
}
final HttpResponse httpResponse = httpClient.execute(request);
final InputStream contentStream = httpResponse.getEntity().getContent();
final String content = IOUtils.toString(contentStream).trim();
if (verbose) {
printResponse(content);
}
}
if (cmd.hasOption("w")) {
final String writable = cmd.getOptionValue("w");
final List<NameValuePair> qparams = new ArrayList<NameValuePair>();
qparams.add(new BasicNameValuePair("userName", userName));
qparams.add(new BasicNameValuePair("password", password));
qparams.add(new BasicNameValuePair("writable", writable));
final URI uri = URIUtils.createURI("http", serverAddress, -1, SET_REPOSITORIES_WRITABLE, URLEncodedUtils.format(qparams, "UTF-8"), null);
final HttpPut request = new HttpPut();
request.setURI(uri);
if (verbose) {
System.out.println("Setting repository writable[" + writable + "]");
}
final HttpResponse httpResponse = httpClient.execute(request);
final InputStream contentStream = httpResponse.getEntity().getContent();
final String content = IOUtils.toString(contentStream).trim();
if (verbose) {
printResponse(content);
}
}
if (cmd.hasOption("backup")) {
System.out.println("Make sure you have disabled repository writes with [-w false], continue? (y)");
final Scanner scanner = new Scanner(System.in);
final String input = scanner.next();
scanner.close();
if (!"y".equals(input)) {
return;
}
if (verbose) {
System.out.println("Starting backup data");
}
final Set<String> repositoryNames = getRepositoryNames();
for (final String repositoryName : repositoryNames) {
// You could specify repository manually
// if (!"archiveDate_article".equals(repositoryName)) {
// continue;
// }
int requestPageNum = 1;
// Backup interrupt recovery
final List<File> backupFiles = getBackupFiles(repositoryName);
if (!backupFiles.isEmpty()) {
final File latestBackup = backupFiles.get(backupFiles.size() - 1);
final String latestPageSize = getBackupFileNameField(latestBackup.getName(), "${pageSize}");
if (!PAGE_SIZE.equals(latestPageSize)) {
if (verbose) {
System.out.println("Repository [" + repositoryName + "] backup have completed");
}
continue;
}
final String latestPageNum = getBackupFileNameField(latestBackup.getName(), "${pageNum}");
// Prepare for the next page to request
requestPageNum = Integer.parseInt(latestPageNum) + 1;
if (verbose) {
System.out.println("Start tot backup interrupt recovery [pageNum=" + requestPageNum + "]");
}
}
int totalPageCount = requestPageNum + 1;
for (; requestPageNum <= totalPageCount; requestPageNum++) {
final List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("userName", userName));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("repositoryName", repositoryName));
params.add(new BasicNameValuePair("pageNum", String.valueOf(requestPageNum)));
params.add(new BasicNameValuePair("pageSize", PAGE_SIZE));
final URI uri = URIUtils.createURI("http", serverAddress, -1, GET_DATA, URLEncodedUtils.format(params, "UTF-8"), null);
final HttpGet request = new HttpGet(uri);
if (verbose) {
System.out.println("Getting data from repository [" + repositoryName + "] with pagination [pageNum=" + requestPageNum + ", pageSize=" + PAGE_SIZE + "]");
}
final HttpResponse httpResponse = httpClient.execute(request);
final InputStream contentStream = httpResponse.getEntity().getContent();
final String content = IOUtils.toString(contentStream, "UTF-8").trim();
contentStream.close();
if (verbose) {
printResponse(content);
}
final JSONObject resp = new JSONObject(content);
final JSONObject pagination = resp.getJSONObject("pagination");
totalPageCount = pagination.getInt("paginationPageCount");
final JSONArray results = resp.getJSONArray("rslts");
final String backupPath = backupDir.getPath() + File.separatorChar + repositoryName + File.separatorChar + requestPageNum + '_' + results.length() + '_' + System.currentTimeMillis() + ".json";
final File backup = new File(backupPath);
final FileWriter fileWriter = new FileWriter(backup);
IOUtils.write(results.toString(), fileWriter);
fileWriter.close();
if (verbose) {
System.out.println("Backup file[path=" + backupPath + "]");
}
}
}
}
if (cmd.hasOption("restore")) {
System.out.println("Make sure you have enabled repository writes with [-w true], continue? (y)");
final Scanner scanner = new Scanner(System.in);
final String input = scanner.next();
scanner.close();
if (!"y".equals(input)) {
return;
}
if (verbose) {
System.out.println("Starting restore data");
}
final Set<String> repositoryNames = getRepositoryNamesFromBackupDir();
for (final String repositoryName : repositoryNames) {
// You could specify repository manually
// if (!"archiveDate_article".equals(repositoryName)) {
// continue;
// }
final List<File> backupFiles = getBackupFiles(repositoryName);
if (verbose) {
System.out.println("Restoring repository[" + repositoryName + ']');
}
for (final File backupFile : backupFiles) {
final FileReader backupFileReader = new FileReader(backupFile);
final String dataContent = IOUtils.toString(backupFileReader);
backupFileReader.close();
final List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("userName", userName));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("repositoryName", repositoryName));
final URI uri = URIUtils.createURI("http", serverAddress, -1, PUT_DATA, URLEncodedUtils.format(params, "UTF-8"), null);
final HttpPost request = new HttpPost(uri);
final List<NameValuePair> data = new ArrayList<NameValuePair>();
data.add(new BasicNameValuePair("data", dataContent));
final UrlEncodedFormEntity entity = new UrlEncodedFormEntity(data, "UTF-8");
request.setEntity(entity);
if (verbose) {
System.out.println("Data[" + dataContent + "]");
}
final HttpResponse httpResponse = httpClient.execute(request);
final InputStream contentStream = httpResponse.getEntity().getContent();
final String content = IOUtils.toString(contentStream, "UTF-8").trim();
contentStream.close();
if (verbose) {
printResponse(content);
}
final String pageNum = getBackupFileNameField(backupFile.getName(), "${pageNum}");
final String pageSize = getBackupFileNameField(backupFile.getName(), "${pageSize}");
final String backupTime = getBackupFileNameField(backupFile.getName(), "${backupTime}");
final String restoredPath = backupDir.getPath() + File.separatorChar + repositoryName + File.separatorChar + pageNum + '_' + pageSize + '_' + backupTime + '_' + System.currentTimeMillis() + ".json";
final File restoredFile = new File(restoredPath);
backupFile.renameTo(restoredFile);
if (verbose) {
System.out.println("Backup file[path=" + restoredPath + "]");
}
}
}
}
if (cmd.hasOption("v")) {
System.out.println(VERSION);
}
if (cmd.hasOption("h")) {
printHelp(options);
}
// final File backup = new File(backupDir.getPath() + File.separatorChar + repositoryName + pageNum + '_' + pageSize + '_'
// + System.currentTimeMillis() + ".json");
// final FileEntity fileEntity = new FileEntity(backup, "application/json; charset=\"UTF-8\"");
} catch (final ParseException e) {
System.err.println("Parsing args failed, caused by: " + e.getMessage());
printHelp(options);
} catch (final ConnectException e) {
System.err.println("Connection refused");
}
}
use of org.apache.http.NameValuePair in project MtgDesktopCompanion by nicho92.
the class TappedOutDeckSniffer method connect.
@Override
public void connect() throws IOException {
cookieStore = new BasicCookieStore();
httpContext = new BasicHttpContext();
httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
httpclient = HttpClients.custom().setUserAgent(getString("USER_AGENT")).setRedirectStrategy(new LaxRedirectStrategy()).build();
httpclient.execute(new HttpGet("https://tappedout.net/accounts/login/?next=/"), httpContext);
HttpPost login = new HttpPost("https://tappedout.net/accounts/login/");
List<NameValuePair> nvps = new ArrayList<>();
nvps.add(new BasicNameValuePair("next", "/"));
nvps.add(new BasicNameValuePair("username", getString("LOGIN")));
nvps.add(new BasicNameValuePair("PASS", getString("PASS")));
nvps.add(new BasicNameValuePair("csrfmiddlewaretoken", getCookieValue("csrftoken")));
login.setEntity(new UrlEncodedFormEntity(nvps));
login.addHeader("Referer", "https://tappedout.net/accounts/login/?next=/");
login.addHeader("Upgrade-Insecure-Requests", "1");
login.addHeader("Origin", "https://tappedout.net");
HttpResponse resp = httpclient.execute(login, httpContext);
logger.debug("Connection OK : " + getString("LOGIN") + " " + resp.getStatusLine().getStatusCode());
}
Aggregations