Skip to content

Commit

Permalink
refactor: move oxEleven
Browse files Browse the repository at this point in the history
Co-authored-by: Javier Rojas <javier.rojas.blum@gmail.com>
Co-authored-by: Javier Rojas Blum <javier.rojas.blum@gmail.com>
Co-authored-by: Mike Schwartz <mike@gluu.org>
Co-authored-by: William Lowe <willow9886@gmail.com>
Co-authored-by: Yuriy Movchan <Yuriy.Movchan@gmail.com>
Co-authored-by: Yuriy Zabrovarnyy <yzabrovarniy@gmail.com>
Co-authored-by: YuriyZ <yzabrovarniy@gmail.com>
Co-authored-by: ossdhaval <343411+ossdhaval@users.noreply.github.com>
Co-authored-by: yurem <Yuriy.Movchan@gmail.com>

Signed-off-by: moabu <47318409+moabu@users.noreply.github.com>
  • Loading branch information
nynymike authored and moabu committed Oct 14, 2024
1 parent 1b2193f commit 96a6706
Show file tree
Hide file tree
Showing 67 changed files with 6,693 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
/oxShibboleth/ @yurem @uprightech
/oxd/ @duttarnab @yuriyz
/uma-rs/ @yurem @yuriyz
/radius/ @yurem @uprightech
/radius/ @yurem @uprightech
/oxEleven/ @yurem @yuriyz
9 changes: 9 additions & 0 deletions oxEleven/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# IntelliJ IDEA project files
.idea
*.iml
.classpath
.project

# Maven
target
.settings/
94 changes: 94 additions & 0 deletions oxEleven/Client/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>oxeleven-client</artifactId>
<name>oxEleven Client</name>
<packaging>jar</packaging>

<parent>
<artifactId>oxeleven</artifactId>
<groupId>org.gluu</groupId>
<version>4.5.6-SNAPSHOT</version>
</parent>

<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<!-- Needed as we have both junit and testng -->
<failIfNoTests>false</failIfNoTests>
<trimStackTrace>false</trimStackTrace>

<!-- exclude selenium tests -->
<excludes>
<exclude>**/selenium/*</exclude>
<exclude>**/webdriver/*</exclude>
<exclude>**/xml/*</exclude>
</excludes>

<suiteXmlFiles>
<suiteXmlFile>target/test-classes/testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

<dependencies>
<dependency>
<groupId>org.gluu</groupId>
<artifactId>oxeleven-model</artifactId>
</dependency>

<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-json-org </artifactId>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>

<!-- Test -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
164 changes: 164 additions & 0 deletions oxEleven/Client/src/main/java/org/gluu/oxeleven/client/BaseClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* oxEleven is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
*
* Copyright (c) 2016, Gluu
*/

package org.gluu.oxeleven.client;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;

import javax.ws.rs.HttpMethod;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.Response;

import org.jboss.logging.Logger;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Strings;

/**
* @author Javier Rojas Blum
* @version March 20, 2017
*/
public abstract class BaseClient<T extends BaseRequest, V extends BaseResponse> {

private static final Logger LOG = Logger.getLogger(BaseClient.class);

protected String url;
protected BaseRequest request;
protected BaseResponse response;
protected WebTarget webTarget = null;
protected Form requestForm = new Form();
protected Response clientResponse;

public BaseClient(String url) {
this.url = url;
}

public String getUrl() {
return url;
}

protected void addRequestParam(String key, String value) {
if (!Strings.isNullOrEmpty(key) && !Strings.isNullOrEmpty(value)) {
if (HttpMethod.POST.equals(request.getHttpMethod())) {
requestForm.param(key, value);
} else {
webTarget = webTarget.queryParam(key, value);
}
}
}

protected void addRequestParam(String key, Long value) {
if (!Strings.isNullOrEmpty(key) && value != null) {
if (HttpMethod.POST.equals(request.getHttpMethod())) {
requestForm.param(key, value.toString());
} else {
webTarget = webTarget.queryParam(key, value);
}
}
}

protected void addRequestParam(String key, String[] value) {
if (!Strings.isNullOrEmpty(key) && value != null) {
if (HttpMethod.POST.equals(request.getHttpMethod())) {
requestForm.param(key, Arrays.toString(value));
} else {
webTarget.queryParam(key, Arrays.toString(value));
}
}
}

public abstract T getRequest();

public abstract void setRequest(T request);

public abstract V getResponse();

public abstract void setResponse(V response);

public abstract V exec() throws Exception;

public String getRequestAsString() {
StringBuilder sb = new StringBuilder();

try {
URL theUrl = new URL(url);

if (getHttpMethod().equals(HttpMethod.POST)) {
sb.append(HttpMethod.POST).append(" ").append(theUrl.getPath()).append(" HTTP/1.1");
if (!Strings.isNullOrEmpty(request.getContentType())) {
sb.append("\n");
sb.append("Content-Type: ").append(request.getContentType());
}
if (!Strings.isNullOrEmpty(request.getMediaType())) {
sb.append("\n");
sb.append("Accept: ").append(request.getMediaType());
}
sb.append("\n");
sb.append("Host: ").append(theUrl.getHost());

String accessToken = request.getAccessToken();
if (!Strings.isNullOrEmpty(accessToken)) {
sb.append("\n");
sb.append("Authorization: Bearer ").append(accessToken);
}

sb.append("\n");
sb.append("\n");
sb.append(request.getQueryString());
} else if (getHttpMethod().equals(HttpMethod.GET)) {
sb.append("GET ").append(theUrl.getPath());
if (!Strings.isNullOrEmpty(request.getQueryString())) {
sb.append("?").append(request.getQueryString());
}
sb.append(" HTTP/1.1");
sb.append("\n");
sb.append("Host: ").append(theUrl.getHost());

String accessToken = request.getAccessToken();
sb.append("\n");
sb.append("Authorization: Bearer ").append(accessToken);
}
} catch (MalformedURLException e) {
LOG.error(e.getMessage(), e);
}

return sb.toString();
}

public String getResponseAsString() {
StringBuilder sb = new StringBuilder();

if (response != null) {
sb.append("HTTP/1.1 ").append(response.getStatus());
if (response.getHeaders() != null) {
for (String key : response.getHeaders().keySet()) {
sb.append("\n")
.append(key)
.append(": ")
.append(response.getHeaders().get(key).get(0));
}
}
if (response.getEntity() != null) {
sb.append("\n");
sb.append("\n");
sb.append(response.getEntity());
}
}
return sb.toString();
}

public abstract String getHttpMethod();

public static String toPrettyJson(Object object) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* oxEleven is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
*
* Copyright (c) 2016, Gluu
*/

package org.gluu.oxeleven.client;

/**
* @author Javier Rojas Blum
* @version March 20, 2017
*/
public abstract class BaseRequest {

private String contentType;
private String mediaType;
private String httpMethod;
private String accessToken;

public String getContentType() {
return contentType;
}

public void setContentType(String contentType) {
this.contentType = contentType;
}

public String getMediaType() {
return mediaType;
}

public void setMediaType(String mediaType) {
this.mediaType = mediaType;
}

public String getHttpMethod() {
return httpMethod;
}

public void setHttpMethod(String httpMethod) {
this.httpMethod = httpMethod;
}

public String getAccessToken() {
return accessToken;
}

public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}

public abstract String getQueryString();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* oxEleven is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
*
* Copyright (c) 2016, Gluu
*/

package org.gluu.oxeleven.client;

import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;

import org.json.JSONException;
import org.json.JSONObject;

/**
* @author Javier Rojas Blum
* @version March 20, 2017
*/
public abstract class BaseResponse {

protected int status;
protected String location;
protected String entity;
protected MultivaluedMap<String, Object> headers;

public BaseResponse(Response clientResponse) {
if (clientResponse != null) {
status = clientResponse.getStatus();
entity = clientResponse.readEntity(String.class);
headers = clientResponse.getHeaders();
if (clientResponse.getLocation() != null) {
location = clientResponse.getLocation().toString();
}
}
}

public int getStatus() {
return status;
}

public String getLocation() {
return location;
}

public String getEntity() {
return entity;
}

public MultivaluedMap<String, Object> getHeaders() {
return headers;
}

public JSONObject getJSONEntity() {
if (entity != null) {
try {
JSONObject jsonObject = new JSONObject(entity);
return jsonObject;
} catch (JSONException e) {
e.printStackTrace();
}
}

return null;
}
}
Loading

0 comments on commit 96a6706

Please sign in to comment.