Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sap.ngom.collection.client;
- import java.io.File;
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.net.URL;
- import java.net.URLEncoder;
- import org.cloudfoundry.uaa.tokens.AbstractToken;
- import org.cloudfoundry.uaa.tokens.GetTokenByPasswordResponse;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.http.HttpHeaders;
- import org.springframework.http.HttpMethod;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.MediaType;
- import org.springframework.http.RequestEntity;
- import org.springframework.http.ResponseEntity;
- import org.springframework.stereotype.Service;
- import org.springframework.web.client.RestOperations;
- import com.fasterxml.jackson.core.JsonParseException;
- import com.fasterxml.jackson.databind.JsonMappingException;
- import com.fasterxml.jackson.databind.JsonNode;
- import com.fasterxml.jackson.databind.ObjectMapper;
- @Service
- public class ElasticSearchClient {
- private static final String CF_TOKEN_URL = "https://uaa.cf.us10.hana.ondemand.com";
- private static final String CF_LOGS_URL = "https://logs.cf.us10.hana.ondemand.com";
- private static ObjectMapper mapper = new ObjectMapper();
- String username = System.getenv("USER");
- String password = System.getenv("PWD");
- String token = "";
- @Autowired
- private RestOperations restOperations;
- public JsonNode getLogsByElasticSearch() throws JsonParseException, JsonMappingException, IOException{
- String query = mapper.readValue(new File(getFileURI("/query.json")), JsonNode.class).toString();
- String index = mapper.readValue(new File(getFileURI("/index.json")), JsonNode.class).toString();
- String queryBody = index + "n" + query + "n" + index + "n" + query;
- ResponseEntity responseEntity = retrieveLog(queryBody);
- if (responseEntity.getStatusCode() == HttpStatus.FOUND){
- dealWithRedirect(responseEntity);
- }
- return null;
- }
- private void dealWithRedirect(ResponseEntity responseEntity){
- HttpHeaders responseHeaders = responseEntity.getHeaders();
- String loction = responseHeaders.get("Location").toString();
- String loginUrl = loction.substring(1, loction.length()-1);
- String setCookie = responseHeaders.get("Set-Cookie").toString();
- setCookie = setCookie.substring(1, loction.length()-1);
- HttpHeaders loginHeaders = new HttpHeaders();
- loginHeaders.set("Cookie", setCookie);
- loginHeaders.set(HttpHeaders.AUTHORIZATION, "Basic Y2Y6");
- loginHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
- String body = "grant_type=password&username=jane.wang03@sap.com&password="+ System.getenv("PWD") + "&response_type=user_token";
- RequestEntity<String> loginRequestEntity = new RequestEntity<>(body, loginHeaders, HttpMethod.POST, URI.create(loginUrl));
- ResponseEntity loginResponse = restOperations.exchange(loginRequestEntity, Object.class);
- String temp = "";
- }
- private void passAuthentication(String state){
- String url = CF_TOKEN_URL + "/oauth/authorize?grant_type=authorization_code&client_id=sleeve-app-logs&response_type=code&state=" + state;
- HttpHeaders headers = new HttpHeaders();
- headers.set(HttpHeaders.AUTHORIZATION, "bearer " + token);
- RequestEntity<String> requestEntity = new RequestEntity<>(headers, HttpMethod.GET, URI.create(url));
- HttpStatus responseEntity = restOperations.exchange(requestEntity, Object.class).getStatusCode();
- }
- private ResponseEntity retrieveLog(String queryBody){
- if (token == ""){
- token = getToken();
- }
- HttpHeaders headers = new HttpHeaders();
- headers.set(HttpHeaders.AUTHORIZATION, "bearer " + token);
- headers.set("kbn-xsrf", "reporting");
- String url = CF_LOGS_URL + "/elasticsearch/_msearch";
- RequestEntity<String> requestEntity = new RequestEntity<>(queryBody, headers, HttpMethod.POST, URI.create(url));
- ResponseEntity responseEntity = restOperations.exchange(requestEntity, Object.class);
- return responseEntity;
- }
- private String getToken() {
- String body = "grant_type=password";
- try {
- body += "&username=" + URLEncoder.encode(username, "UTF-8");
- body += "&password=" + URLEncoder.encode(password, "UTF-8");
- body += "&response_type=user_token";
- } catch (UnsupportedEncodingException ex) {
- throw new RuntimeException(ex);
- }
- HttpHeaders headers = new HttpHeaders();
- headers.set(HttpHeaders.AUTHORIZATION, "Basic Y2Y6");
- headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
- RequestEntity<String> requestEntity = new RequestEntity<>(body, headers, HttpMethod.POST, URI.create(CF_TOKEN_URL + "/oauth/token"));
- ResponseEntity<GetTokenByPasswordResponse> responseEntity = restOperations.exchange(requestEntity, GetTokenByPasswordResponse.class);
- AbstractToken token = responseEntity.getBody();
- return token.getAccessToken();
- }
- private URI getFileURI(String path) {
- URL ruleURL = ElasticSearchClient.class.getResource(path);
- URI uri = null;
- try {
- uri = ruleURL.toURI();
- } catch (URISyntaxException e) {
- e.printStackTrace();
- }
- return uri;
- }
- }
Add Comment
Please, Sign In to add comment