Search in sources :

Example 96 with NameValuePair

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;
}
Also used : RyaURI(org.apache.rya.api.domain.RyaURI) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) URISyntaxException(java.net.URISyntaxException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 97 with NameValuePair

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);
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) ArrayList(java.util.ArrayList) URIImpl(org.openrdf.model.impl.URIImpl) URISyntaxException(java.net.URISyntaxException) URI(org.openrdf.model.URI) RyaURI(org.apache.rya.api.domain.RyaURI) ImmutableMap(com.google.common.collect.ImmutableMap) URIBuilder(org.apache.http.client.utils.URIBuilder) RyaURI(org.apache.rya.api.domain.RyaURI) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) Property(org.apache.rya.indexing.entity.model.Property)

Example 98 with NameValuePair

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);
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) ArrayList(java.util.ArrayList) URIImpl(org.openrdf.model.impl.URIImpl) URISyntaxException(java.net.URISyntaxException) RyaType(org.apache.rya.api.domain.RyaType) URIBuilder(org.apache.http.client.utils.URIBuilder) RyaURI(org.apache.rya.api.domain.RyaURI) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) Value(org.openrdf.model.Value) Property(org.apache.rya.indexing.entity.model.Property)

Example 99 with NameValuePair

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");
    }
}
Also used : Options(org.apache.commons.cli.Options) Scanner(java.util.Scanner) HttpPost(org.apache.http.client.methods.HttpPost) PosixParser(org.apache.commons.cli.PosixParser) HttpGet(org.apache.http.client.methods.HttpGet) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) URI(java.net.URI) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpPut(org.apache.http.client.methods.HttpPut) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) FileReader(java.io.FileReader) CommandLineParser(org.apache.commons.cli.CommandLineParser) ConnectException(java.net.ConnectException) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) InputStream(java.io.InputStream) JSONArray(org.json.JSONArray) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) CommandLine(org.apache.commons.cli.CommandLine) JSONObject(org.json.JSONObject) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) ParseException(org.apache.commons.cli.ParseException) File(java.io.File)

Example 100 with NameValuePair

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());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpGet(org.apache.http.client.methods.HttpGet) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) LaxRedirectStrategy(org.apache.http.impl.client.LaxRedirectStrategy)

Aggregations

NameValuePair (org.apache.http.NameValuePair)713 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)592 ArrayList (java.util.ArrayList)478 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)256 HttpPost (org.apache.http.client.methods.HttpPost)218 HttpResponse (org.apache.http.HttpResponse)150 IOException (java.io.IOException)125 HttpEntity (org.apache.http.HttpEntity)108 Test (org.junit.Test)85 URI (java.net.URI)79 Map (java.util.Map)72 HashMap (java.util.HashMap)68 HttpGet (org.apache.http.client.methods.HttpGet)66 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)59 UnsupportedEncodingException (java.io.UnsupportedEncodingException)58 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)55 URISyntaxException (java.net.URISyntaxException)52 Document (org.jsoup.nodes.Document)51 URIBuilder (org.apache.http.client.utils.URIBuilder)50 HttpClient (org.apache.http.client.HttpClient)46