-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(connector-domparser): add okHttp web client impl
- Loading branch information
Showing
2 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...thub/thetric/iliasdownloader/service/webparser/impl/webclient/OkHttpIliasWebClient.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.github.thetric.iliasdownloader.service.webparser.impl.webclient | ||
|
||
import com.github.thetric.iliasdownloader.service.exception.IliasException | ||
import com.github.thetric.iliasdownloader.service.model.LoginCredentials | ||
import groovy.transform.CompileStatic | ||
import groovy.util.logging.Log4j2 | ||
import okhttp3.* | ||
|
||
@Log4j2 | ||
@CompileStatic | ||
final class OkHttpIliasWebClient implements IliasWebClient { | ||
private final OkHttpClient client | ||
private final CookieManager cookieManager | ||
|
||
private final String iliasBaseUrl | ||
private final String clientId | ||
|
||
private final String loginPage | ||
private final String logoutPage | ||
|
||
OkHttpIliasWebClient(final String iliasBaseUrl, final String clientId) { | ||
this.iliasBaseUrl = iliasBaseUrl | ||
this.clientId = clientId | ||
loginPage = "${iliasBaseUrl}login.php" | ||
logoutPage = "${iliasBaseUrl}logout.php" | ||
|
||
cookieManager = new CookieManager() | ||
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL) | ||
client = new OkHttpClient.Builder().cookieJar(new JavaNetCookieJar(cookieManager)) | ||
.followRedirects(false) | ||
.build() | ||
} | ||
|
||
@Override | ||
void login(final LoginCredentials credentials) { | ||
log.info('Logging in at {}', loginPage) | ||
final loginForm = new FormBody.Builder().add('username', credentials.userName) | ||
.add('password', credentials.password) | ||
.build() | ||
final Request request = new Request.Builder().url(loginPage) | ||
.post(loginForm) | ||
.build() | ||
def loginClient = client.newBuilder().followRedirects(true).build() | ||
final Response response = loginClient.newCall(request).execute() | ||
checkResponse(loginPage, response) | ||
log.info('Login at {} succeeded', loginPage) | ||
} | ||
|
||
@Override | ||
void logout() { | ||
log.info('Logging out: {}', logoutPage) | ||
final response = executeGetRequest(logoutPage) | ||
// TODO check response | ||
cookieManager.cookieStore.removeAll() | ||
checkResponse(logoutPage, response) | ||
log.info('Logout at {} succeeded', logoutPage) | ||
} | ||
|
||
private Response executeGetRequest(final String url) { | ||
final Request request = new Request.Builder().url(url).build() | ||
return client.newCall(request).execute() | ||
} | ||
|
||
@Override | ||
String getHtml(final String url) { | ||
final response = executeGetRequest(url) | ||
checkResponse(url, response) | ||
return response.body().string() | ||
} | ||
|
||
@Override | ||
InputStream getAsInputStream(final String url) { | ||
final Response response = executeGetRequest(url) | ||
checkResponse(url, response) | ||
return response.body().byteStream() | ||
} | ||
|
||
private void checkResponse(final String url, final Response response) { | ||
if (!response.successful) { | ||
throw new IliasException("Failed to GET $url: ${response.message()}") | ||
} | ||
} | ||
|
||
} |