Java Code Examples for org.eclipse.jetty.client.api.Request#method()

The following examples show how to use org.eclipse.jetty.client.api.Request#method() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BloomFilterPersistScalarFunction.java    From presto-bloomfilter with Apache License 2.0 6 votes vote down vote up
@SqlType(StandardTypes.BOOLEAN)
@Nullable
@SqlNullable
public static Boolean bloomFilterPersist(@SqlNullable @SqlType(BloomFilterType.TYPE) Slice bloomFilterSlice, @SqlType(StandardTypes.VARCHAR) Slice urlSlice) throws Exception
{
    // Nothing todo
    if (urlSlice == null) {
        return true;
    }
    BloomFilter bf = getOrLoadBloomFilter(bloomFilterSlice);

    // Persist
    // we do not try catch here to make sure that errors are communicated clearly to the client
    // and typical retry logic continues to work
    String url = new String(urlSlice.getBytes());
    if (!HTTP_CLIENT.isStarted()) {
        log.warn("Http client was not started, trying to start");
        HTTP_CLIENT.start();
    }
    Request post = HTTP_CLIENT.POST(url);
    post.content(new StringContentProvider(new String(bf.toBase64())));
    post.method("PUT");
    post.send();
    log.info("Persisted " + bf.toString() + " " + url);
    return true;
}
 
Example 2
Source File: BloomFilter.java    From presto-bloomfilter with Apache License 2.0 6 votes vote down vote up
public static BloomFilter fromUrl(String url) throws Exception
{
    log.info("Loading bloom filter from " + url);

    Request request = BloomFilterScalarFunctions.HTTP_CLIENT.newRequest(url);
    request.method("GET");
    InputStreamResponseListener listener = new InputStreamResponseListener();
    request.send(listener);

    // Wait for the response headers to arrive
    Response response = listener.get(10, TimeUnit.SECONDS);

    // Look at the response
    if (response.getStatus() == 200) {
        // Use try-with-resources to close input stream.
        try (InputStream responseContent = listener.getInputStream()) {
            byte[] bytes = ByteStreams.toByteArray(responseContent);
            return newInstance(bytes);
        }
    }
    log.warn("Non-200 response status " + response.getStatus());
    return null;
}
 
Example 3
Source File: ProxyServiceImpl.java    From moon-api-gateway with MIT License 4 votes vote down vote up
/**
 * Create a new request object based on the variables created in prepareProxyInterceptor.
 *
 * Determine the request Method.
 * Inject the header and query param into the new request object.
 * It also injects the body sent by the client into a byte array.
 *
 * @param request This is a client request.
 * @param responseInfo It is an object created by prepareProxyInterceptor. Contains the header, query, and body required for the proxy.
 * @return a new request object that is completely different from client request. This will request an api for the outbound service.
 */

private static Request setHeaderAndQueryInfo(Request request, ResponseInfo responseInfo) {
    Map<String, String> requestHeaders = responseInfo.getHeaders();

    requestHeaders.forEach(request::header);

    request.method(responseInfo.getRequestMethod());
    request.accept(responseInfo.getRequestAccept());

    if (Strings.isNotEmpty(responseInfo.getRequestContentType()) && Objects.nonNull(responseInfo.getRequestBody())) {
        request.content(new BytesContentProvider(responseInfo.getRequestBody()), responseInfo.getRequestContentType());
    }

    Map<String, String> requestQueryParams = responseInfo.getQueryStringMap();
    requestQueryParams.forEach(request::param);

    return request;
}
 
Example 4
Source File: HTTPAction.java    From nadia with Apache License 2.0 4 votes vote down vote up
@Override
public HashMap<String, String> execute(Frame frame) {
	
	 	/*
	 	 * WikiAPI: http://www.mediawiki.org/wiki/Extension:MobileFrontend
	 	 * e.g.,
	 	 * url="http://en.wikipedia.org/w/api.php?format=xml&action=query&prop=extracts&explaintext&exsentences=3&titles=Edinburgh";
	 	 * xpath="//extract";
	 	 * 
	 	 * or
	 	 * 
	 	 * curl --data "state=on" http://mmt.et.hs-wismar.de:8080/Lightbulb/Lightbulb
	 	 * 
	 	 */
	
		String replaced_url=replaceSlotMarkers(url, frame);
		String replaced_params=replaceSlotMarkers(params, frame);
		String[] params_arr = replaced_params.split("&");
		
		String result="Sorry, that did not work. ";
        try{
        		        	 
        	ContentResponse response;
        	Request request;
       		
        	request = client.newRequest(replaced_url);

        	//choose method
        	if(method.toLowerCase().equals("get")){
        		request.method(HttpMethod.GET);
        	}
        	else{
        		request.method(HttpMethod.POST);
        	}
        	
        	//process parameters
        	String[] key_value;
        	for(String paramPair : params_arr){
        		key_value=paramPair.split("=");
        		if(key_value.length>1) request.param(key_value[0],key_value[1]);
        		else request.param(key_value[0], "");
        	}       	
        	
        	logger.info("requesting: "+request.getURI()+", "+request.getParams().toString());
        	response = request.send();
        	logger.info("HTTP status: "+response.getStatus());
        	
        	String xml = response.getContentAsString();
        	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
	        DocumentBuilder builder = factory.newDocumentBuilder();
        	Document doc = builder.parse(new InputSource(new StringReader(xml)));
        	 
	        XPathFactory xPathfactory = XPathFactory.newInstance();
	        XPath xPath = xPathfactory.newXPath();
	        XPathExpression expr = xPath.compile(xpath);
	        result = (String) expr.evaluate(doc, XPathConstants.STRING);
	        
	        //Postprocessing
	        result = result.replaceAll("\\s\\(.*?\\)", ""); //remove content in brackets
	        result = result.replaceAll("\\s\\[.*?\\]", "");
        }
        catch(Exception ex){
        	ex.printStackTrace();
        }
        executionResults.put("result", result);
		return executionResults;
}