Add : Attendance Module + QVM Module
This commit is contained in:
@@ -22,12 +22,12 @@ import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.InetAddress;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.List;
|
||||
import java.net.URL;
|
||||
import wslite.json.JSONException;
|
||||
import wslite.json.JSONObject;
|
||||
|
||||
import org.apache.batik.util.io.StringDecoder;
|
||||
|
||||
@@ -48,9 +48,6 @@ import com.axelor.meta.schema.actions.ActionView;
|
||||
import com.axelor.rpc.ActionRequest;
|
||||
import com.axelor.rpc.ActionResponse;
|
||||
import com.google.inject.Singleton;
|
||||
import com.mongodb.util.JSON;
|
||||
|
||||
import net.minidev.json.JSONObject;
|
||||
|
||||
@Singleton
|
||||
public class AppBaseController {
|
||||
@@ -174,4 +171,69 @@ public class AppBaseController {
|
||||
Process p = Runtime.getRuntime().exec(args);
|
||||
// BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
||||
}
|
||||
|
||||
public static String getAccessToken() throws Exception {
|
||||
AppSettings appSettings = AppSettings.get();
|
||||
String BASE_URL = appSettings.get("portail.api.baseurl");
|
||||
String username = appSettings.get("portail.api.user");
|
||||
String password = appSettings.get("portail.api.password");
|
||||
|
||||
String endpoint = BASE_URL + "token/";
|
||||
URL url = new URL(endpoint);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
|
||||
conn.setRequestMethod("POST");
|
||||
conn.setRequestProperty("Content-Type", "application/json");
|
||||
conn.setRequestProperty("Accept", "application/json");
|
||||
conn.setDoOutput(true);
|
||||
|
||||
// Create a JSON object for the input
|
||||
JSONObject jsonInput = new JSONObject();
|
||||
jsonInput.put("username", username);
|
||||
jsonInput.put("password", password);
|
||||
|
||||
// Write the JSON input to the output stream
|
||||
try (OutputStream os = conn.getOutputStream()) {
|
||||
byte[] input = jsonInput.toString().getBytes("utf-8");
|
||||
os.write(input, 0, input.length);
|
||||
}
|
||||
|
||||
int responseCode = conn.getResponseCode();
|
||||
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||
try (BufferedReader br =
|
||||
new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"))) {
|
||||
StringBuilder response = new StringBuilder();
|
||||
String responseLine;
|
||||
while ((responseLine = br.readLine()) != null) {
|
||||
response.append(responseLine.trim());
|
||||
}
|
||||
return parseAccessToken(response.toString());
|
||||
}
|
||||
} else {
|
||||
// Read the error stream for more details
|
||||
StringBuilder errorResponse = new StringBuilder();
|
||||
try (BufferedReader br =
|
||||
new BufferedReader(new InputStreamReader(conn.getErrorStream(), "utf-8"))) {
|
||||
String errorLine;
|
||||
while ((errorLine = br.readLine()) != null) {
|
||||
errorResponse.append(errorLine.trim());
|
||||
}
|
||||
}
|
||||
throw new RuntimeException(
|
||||
"Failed: HTTP error code: " + responseCode + ", Error: " + errorResponse.toString());
|
||||
}
|
||||
}
|
||||
|
||||
// Parse the JSON response to extract the access token
|
||||
private static String parseAccessToken(String jsonResponse) {
|
||||
try {
|
||||
JSONObject jsonObject = new JSONObject(jsonResponse);
|
||||
return jsonObject.get("access").toString(); // Extract the access token
|
||||
} catch (JSONException e) {
|
||||
System.err.println("Failed to parse JSON: " + jsonResponse);
|
||||
e.printStackTrace();
|
||||
return null; // Handle error appropriately
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user