Skip to content

Commit

Permalink
feat(connector-domparser): add okHttp web client impl
Browse files Browse the repository at this point in the history
  • Loading branch information
thetric committed Apr 9, 2017
1 parent af52890 commit 3e27d9c
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.github.thetric.iliasdownloader.service.webparser.impl.course.jsoup.JS
import com.github.thetric.iliasdownloader.service.webparser.impl.util.WebIoExceptionTranslator
import com.github.thetric.iliasdownloader.service.webparser.impl.util.WebIoExceptionTranslatorImpl
import com.github.thetric.iliasdownloader.service.webparser.impl.webclient.IliasWebClient
import com.github.thetric.iliasdownloader.service.webparser.impl.webclient.OkHttpIliasWebClient
import groovy.transform.CompileStatic

@CompileStatic
Expand Down Expand Up @@ -61,7 +62,7 @@ final class WebParserIliasServiceProvider implements IliasServiceProvider {
IliasService newInstance() {
WebIoExceptionTranslator webIoExceptionTranslator = new WebIoExceptionTranslatorImpl()
JSoupParserService jSoupParserService = new JSoupParserServiceImpl()
final IliasWebClient iliasWebClient = null
final IliasWebClient iliasWebClient = new OkHttpIliasWebClient(iliasBaseUrl, clientId)
CourseSyncService courseSyncServiceProvider = new CourseSyncServiceImpl(
webIoExceptionTranslator,
jSoupParserService,
Expand Down
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()}")
}
}

}

0 comments on commit 3e27d9c

Please sign in to comment.