Page History
...
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
package rest.code.examples; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Random; import org.apache.hc.client5.http.entity.mime.HttpMultipartMode; import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder; import org.apache.hc.client5.http.fluent.Content; import org.apache.hc.client5.http.fluent.Request; import org.apache.hc.core5.http.ContentType; import org.apache.hc.core5.http.HttpEntity; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; /** * Upload License File via the Yellowfin REST API */ public class UploadALicenseFile { public static void main(String[] args) throws Exception { System.out.println("Upload License File"); String host = "http://localhost:8080/Yellowfin"; String restUsername = "admin@yellowfin.com.au"; String restPassword = "test"; String fileToImport = "/Downloads/Yellowfin-License.lic"; Path licenseFile = Paths.get(fileToImport); byte[] fileContents = Files.readAllBytes(licenseFile); String token = generateToken(host, restUsername, restPassword); HttpEntity newLicenseFileMulitpartEntity = MultipartEntityBuilder .create() .setMode(HttpMultipartMode.LEGACY) .setCharset(Charset.forName("UTF-8")) .addBinaryBody("newLicence", fileContents , ContentType.DEFAULT_BINARY, licenseFile.getFileName().toString()) .build(); System.out.println("Upload License Content"); Content uploadLicenseContent = Request.post(host + "/api/rpc/licence-management/upload-licence") .addHeader("Authorization", "YELLOWFIN ts=" + System.currentTimeMillis() + " , nonce=" + new Random().nextLong() + ", token=" + token) .addHeader("Accept", "application/vnd.yellowfin.api-v1+json") .addHeader("Content-Type", newLicenseFileMulitpartEntity.getContentType()) .addHeader("cache-control", "no-cache") .body(newLicenseFileMulitpartEntity) .execute() .returnContent(); System.out.println("License Upload Complete"); System.out.println(uploadLicenseContent.asString()); } /* * This function generates an access token for a user that will grant them access to * call REST API endpoints. */ public static String generateToken(String host, String username, String password) throws IOException { Content c = Request.post(host + "/api/refresh-tokens") .addHeader("Authorization", "YELLOWFIN ts=" + System.currentTimeMillis() + " , nonce=" + new Random().nextLong()) .addHeader("Accept", "application/vnd.yellowfin.api-v1+json") .addHeader("Content-Type", "application/json") .bodyString("{ \"userName\": \""+ username + "\",\"password\": \""+ password + "\"}", null) .execute().returnContent(); JsonObject jsonObject = new JsonParser().parse(c.asString()).getAsJsonObject(); JsonElement accessToken = jsonObject.getAsJsonObject("_embedded").getAsJsonObject("accessToken").get("securityToken"); if (accessToken!=null) { System.out.println("Access Token: " + accessToken); } else { System.out.println("Token not retrieved successfully"); System.exit(-1); } return accessToken.getAsString(); } } |
...
Code Block | ||||
---|---|---|---|---|
| ||||
package main import ( "bytes" "encoding/json" "fmt" "io" "io/ioutil" "math/rand" "mime/multipart" "net/http" "os" "path/filepath" "time" ) func main() { fmt.Println("Upload License File") host := "http://localhost:8080/Yellowfin" restUsername := "admin@yellowfin.com.au" restPassword := "test" fileToImport := "/Downloads/Yellowfin-License.lic" filePath := filepath.Clean(fileToImport) token, err := generateToken(host, restUsername, restPassword) if err != nil { fmt.Println("Error generating token:", err) return } file, err := os.Open(filePath) if err != nil { fmt.Println("Error opening file:", err) return } defer file.Close() body := &bytes.Buffer{} writer := multipart.NewWriter(body) part, err := writer.CreateFormFile("newLicence", filepath.Base(filePath)) if err != nil { fmt.Println("Error writing to buffer:", err) return } _, err = io.Copy(part, file) if err != nil { fmt.Println("Error copying file to buffer:", err) return } err = writer.Close() if err != nil { fmt.Println("Error closing writer:", err) return } nonce := rand.Int63() req, err := http.NewRequest("POST", host+"/api/rpc/licence-management/upload-licence", body) if err != nil { fmt.Println("Error creating request:", err) return } req.Header.Set("Authorization", fmt.Sprintf("YELLOWFIN ts=%d, nonce=%d, token=%s", time.Now().UnixMilli(), nonce, token)) req.Header.Set("Accept", "application/vnd.yellowfin.api-v1+json") req.Header.Set("Content-Type", writer.FormDataContentType()) client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Println("Error sending request:", err) return } defer resp.Body.Close() fmt.Println("License Upload Complete") responseBody, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println("Error reading response body:", err) return } fmt.Println(string(responseBody)) } func generateToken(host, restUsername, restPassword string) (string, error) { nonce := rand.Int63() reqBody := fmt.Sprintf(`{"userName": "%s", "password": "%s"}`, restUsername, restPassword) req, err := http.NewRequest("POST", host+"/api/refresh-tokens", bytes.NewBufferString(reqBody)) if err != nil { return "", err } req.Header.Set("Authorization", fmt.Sprintf("YELLOWFIN ts=%d, nonce=%d", time.Now().UnixMilli(), nonce)) req.Header.Set("Accept", "application/vnd.yellowfin.api-v1+json") req.Header.Set("Content-Type", "application/json") client := &http.Client{} resp, err := client.Do(req) if err != nil { return "", err } defer resp.Body.Close() respBody, err := ioutil.ReadAll(resp.Body) if err != nil { return "", err } var jsonResponse map[string]interface{} if err := json.Unmarshal(respBody, &jsonResponse); err != nil { return "", err } accessToken, ok := jsonResponse["_embedded"].(map[string]interface{})["accessToken"].(map[string]interface{})["securityToken"].(string) if !ok { return "", fmt.Errorf("Token not retrieved successfully") } return accessToken, nil } |
...