forked from github/wulkanowy-mirror
Add api to retrieve messages (#51)
This commit is contained in:
parent
5b52b5d398
commit
f93816910d
@ -30,6 +30,7 @@ jacocoTestReport {
|
||||
dependencies {
|
||||
implementation 'org.jsoup:jsoup:1.10.3'
|
||||
implementation 'org.apache.commons:commons-lang3:3.7'
|
||||
implementation 'com.google.code.gson:gson:2.8.2'
|
||||
|
||||
testImplementation 'junit:junit:4.12'
|
||||
testImplementation 'org.mockito:mockito-core:2.13.0'
|
||||
|
@ -41,13 +41,45 @@ public class Client {
|
||||
connection.data(data[0], data[1]);
|
||||
}
|
||||
|
||||
Connection.Response response = connection.cookies(getCookies())
|
||||
Connection.Response response = connection
|
||||
.followRedirects(true)
|
||||
.method(Connection.Method.POST)
|
||||
.cookies(getCookies())
|
||||
.execute();
|
||||
|
||||
this.cookies.addItems(response.cookies());
|
||||
|
||||
return response.parse();
|
||||
}
|
||||
|
||||
public String getJsonStringByUrl(String url) throws IOException {
|
||||
Connection.Response response = Jsoup.connect(url)
|
||||
.followRedirects(true)
|
||||
.ignoreContentType(true)
|
||||
.cookies(getCookies())
|
||||
.execute();
|
||||
|
||||
this.cookies.addItems(response.cookies());
|
||||
|
||||
return response.body();
|
||||
}
|
||||
|
||||
public String postJsonStringByUrl(String url, String[][] params) throws IOException {
|
||||
Connection connection = Jsoup.connect(url);
|
||||
|
||||
for (String[] data : params) {
|
||||
connection.data(data[0], data[1]);
|
||||
}
|
||||
|
||||
Connection.Response response = connection
|
||||
.followRedirects(true)
|
||||
.ignoreContentType(true)
|
||||
.method(Connection.Method.POST)
|
||||
.cookies(getCookies())
|
||||
.execute();
|
||||
|
||||
this.cookies.addItems(response.cookies());
|
||||
|
||||
return response.body();
|
||||
}
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ import io.github.wulkanowy.api.login.Login;
|
||||
import io.github.wulkanowy.api.login.LoginErrorException;
|
||||
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
|
||||
import io.github.wulkanowy.api.login.VulcanOfflineException;
|
||||
import io.github.wulkanowy.api.messages.Messages;
|
||||
import io.github.wulkanowy.api.notes.AchievementsList;
|
||||
import io.github.wulkanowy.api.notes.NotesList;
|
||||
import io.github.wulkanowy.api.school.SchoolInfo;
|
||||
@ -111,8 +112,6 @@ public class Vulcan {
|
||||
|
||||
snp.storeContextCookies();
|
||||
|
||||
// this.cookies = client.getCookiesObject();
|
||||
|
||||
return snp;
|
||||
}
|
||||
|
||||
@ -171,4 +170,8 @@ public class Vulcan {
|
||||
public FamilyInformation getFamilyInformation() throws IOException, NotLoggedInErrorException {
|
||||
return new FamilyInformation(getStudentAndParent());
|
||||
}
|
||||
|
||||
public Messages getMessages() {
|
||||
return new Messages(client, getProtocolSchema(), getLogHost(), symbol);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,4 @@
|
||||
package io.github.wulkanowy.api.messages;
|
||||
|
||||
class BadRequestException extends Exception {
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package io.github.wulkanowy.api.messages;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
public class Message {
|
||||
|
||||
@SerializedName("Nieprzeczytana")
|
||||
public boolean unread;
|
||||
|
||||
@SerializedName("Data")
|
||||
public String date;
|
||||
|
||||
@SerializedName("Tresc")
|
||||
public String content;
|
||||
|
||||
@SerializedName("Temat")
|
||||
public String subject;
|
||||
|
||||
@SerializedName("NadawcaNazwa")
|
||||
public String sender;
|
||||
|
||||
@SerializedName("IdWiadomosci")
|
||||
public int messageID;
|
||||
|
||||
@SerializedName("IdNadawca")
|
||||
public int senderID;
|
||||
|
||||
@SerializedName("Id")
|
||||
public int id;
|
||||
}
|
113
api/src/main/java/io/github/wulkanowy/api/messages/Messages.java
Normal file
113
api/src/main/java/io/github/wulkanowy/api/messages/Messages.java
Normal file
@ -0,0 +1,113 @@
|
||||
package io.github.wulkanowy.api.messages;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonParseException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import io.github.wulkanowy.api.Client;
|
||||
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
|
||||
|
||||
public class Messages {
|
||||
|
||||
private static final String BASE_URL = "{schema}://uonetplus-uzytkownik.{host}/{symbol}/";
|
||||
|
||||
private static final String LIST_BASE_URL = BASE_URL + "Wiadomosc.mvc/";
|
||||
|
||||
private static final String RECEIVED_URL = LIST_BASE_URL + "GetWiadomosciOdebrane";
|
||||
|
||||
private static final String SENT_URL = LIST_BASE_URL + "GetWiadomosciWyslane";
|
||||
|
||||
private static final String DELETED_URL = LIST_BASE_URL + "GetWiadomosciUsuniete";
|
||||
|
||||
private static final String MESSAGE_URL = LIST_BASE_URL + "GetTrescWiadomosci";
|
||||
|
||||
public static final int RECEIVED_FOLDER = 1;
|
||||
|
||||
public static final int SENT_FOLDER = 2;
|
||||
|
||||
public static final int DELETED_FOLDER = 3;
|
||||
|
||||
private static final String ERROR_TITLE = "Błąd strony";
|
||||
|
||||
private Client client;
|
||||
|
||||
private String schema;
|
||||
|
||||
private String host;
|
||||
|
||||
private String symbol;
|
||||
|
||||
public Messages(Client client, String schema, String host, String symbol) {
|
||||
this.client = client;
|
||||
this.schema = schema;
|
||||
this.host = host;
|
||||
this.symbol = symbol;
|
||||
}
|
||||
|
||||
private String getFilledUrl(String url) {
|
||||
return url.replace("{schema}", schema)
|
||||
.replace("{host}", host)
|
||||
.replace("{symbol}", symbol);
|
||||
}
|
||||
|
||||
public List<Message> getReceived() throws IOException, NotLoggedInErrorException, BadRequestException {
|
||||
return getMessages(RECEIVED_URL);
|
||||
}
|
||||
|
||||
public List<Message> getSent() throws IOException, NotLoggedInErrorException, BadRequestException {
|
||||
return getMessages(SENT_URL);
|
||||
}
|
||||
|
||||
public List<Message> getDeleted() throws IOException, NotLoggedInErrorException, BadRequestException {
|
||||
return getMessages(DELETED_URL);
|
||||
}
|
||||
|
||||
private List<Message> getMessages(String url) throws IOException, NotLoggedInErrorException, BadRequestException {
|
||||
String res = client.getJsonStringByUrl(getFilledUrl(url));
|
||||
|
||||
List<Message> messages;
|
||||
|
||||
try {
|
||||
messages = new Gson().fromJson(res, MessagesContainer.class).data;
|
||||
} catch (JsonParseException e) {
|
||||
if (res.contains(ERROR_TITLE)) {
|
||||
throw new BadRequestException();
|
||||
}
|
||||
|
||||
throw new NotLoggedInErrorException();
|
||||
}
|
||||
|
||||
return messages;
|
||||
}
|
||||
|
||||
public Message getMessage(int id, int folder) throws IOException, BadRequestException, NotLoggedInErrorException {
|
||||
String res = client.postJsonStringByUrl(MESSAGE_URL, new String[][]{
|
||||
{"idWiadomosc", String.valueOf(id)},
|
||||
{"Folder", String.valueOf(folder)}
|
||||
});
|
||||
|
||||
Message message;
|
||||
|
||||
try {
|
||||
message = new Gson().fromJson(res, MessageContainer.class).data;
|
||||
} catch (JsonParseException e) {
|
||||
if (res.contains(ERROR_TITLE)) {
|
||||
throw new BadRequestException();
|
||||
}
|
||||
|
||||
throw new NotLoggedInErrorException();
|
||||
}
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
public static class MessagesContainer {
|
||||
public List<Message> data;
|
||||
}
|
||||
|
||||
public static class MessageContainer {
|
||||
public Message data;
|
||||
}
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
package io.github.wulkanowy.api.messages;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import io.github.wulkanowy.api.Client;
|
||||
import io.github.wulkanowy.api.FixtureHelper;
|
||||
import io.github.wulkanowy.api.login.NotLoggedInErrorException;
|
||||
|
||||
public class MessagesTest {
|
||||
|
||||
private Client getFixtureAsString(String fixtureFileName) throws Exception {
|
||||
Client client = Mockito.mock(Client.class);
|
||||
Mockito.when(client.getJsonStringByUrl(Mockito.anyString()))
|
||||
.thenReturn(FixtureHelper.getAsString(getClass().getResourceAsStream(fixtureFileName)));
|
||||
Mockito.when(client.postJsonStringByUrl(Mockito.anyString(), Mockito.any()))
|
||||
.thenReturn(FixtureHelper.getAsString(getClass().getResourceAsStream(fixtureFileName)));
|
||||
return client;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMessages() throws Exception {
|
||||
Client client = getFixtureAsString("GetWiadomosciOdebrane.json");
|
||||
|
||||
Messages messages = new Messages(client, "", "", "");
|
||||
List<Message> messageList = messages.getReceived();
|
||||
|
||||
Assert.assertEquals(true, messageList.get(1).unread);
|
||||
Assert.assertEquals("2016-03-15 09:00:00", messageList.get(0).date);
|
||||
Assert.assertEquals(null, messageList.get(0).content);
|
||||
Assert.assertEquals("Kowalski Jan", messageList.get(0).sender);
|
||||
Assert.assertEquals(12347, messageList.get(2).id);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMessagesEmpty() throws Exception {
|
||||
Client client = getFixtureAsString("GetWiadomosciUsuniete-empty.json");
|
||||
|
||||
Messages messages = new Messages(client, "", "", "");
|
||||
List<Message> messageList = messages.getSent();
|
||||
|
||||
Assert.assertTrue(messageList.isEmpty());
|
||||
}
|
||||
|
||||
@Test(expected = NotLoggedInErrorException.class)
|
||||
public void getMessagesError() throws Exception {
|
||||
Client client = getFixtureAsString("UndefinedError.txt");
|
||||
|
||||
Messages messages = new Messages(client, "", "", "");
|
||||
messages.getDeleted();
|
||||
}
|
||||
|
||||
@Test(expected = BadRequestException.class)
|
||||
public void getMessagesBadRequest() throws Exception {
|
||||
Client client = getFixtureAsString("PageError.html");
|
||||
|
||||
Messages messages = new Messages(client, "", "", "");
|
||||
messages.getDeleted();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getMessage() throws Exception {
|
||||
Client client = getFixtureAsString("GetTrescWiadomosci.json");
|
||||
|
||||
Messages messages = new Messages(client, "", "", "");
|
||||
Message message = messages.getMessage(123, Messages.RECEIVED_FOLDER);
|
||||
Assert.assertEquals(12345, message.id);
|
||||
Assert.assertEquals("Witam, …. \nPozdrawiam Krzysztof Czerkas", message.content);
|
||||
}
|
||||
|
||||
@Test(expected = NotLoggedInErrorException.class)
|
||||
public void getMessageError() throws Exception {
|
||||
Client client = getFixtureAsString("UndefinedError.txt");
|
||||
|
||||
Messages messages = new Messages(client, "", "", "");
|
||||
messages.getMessage(321, Messages.SENT_FOLDER);
|
||||
}
|
||||
|
||||
@Test(expected = BadRequestException.class)
|
||||
public void getMessageBadRequest() throws Exception {
|
||||
Client client = getFixtureAsString("PageError.html");
|
||||
|
||||
Messages messages = new Messages(client, "", "", "");
|
||||
messages.getMessage(1, Messages.DELETED_FOLDER);
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
{
|
||||
"success": true,
|
||||
"data": {
|
||||
"Id": 12345,
|
||||
"Tresc": "Witam, …. \nPozdrawiam Krzysztof Czerkas"
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
{
|
||||
"success": true,
|
||||
"data": [
|
||||
{
|
||||
"Nieprzeczytana": false,
|
||||
"Data": "2016-03-15 09:00:00",
|
||||
"Tresc": null,
|
||||
"Temat": "Wycieczka",
|
||||
"NadawcaNazwa": "Kowalski Jan",
|
||||
"IdWiadomosci": 1234,
|
||||
"IdNadawca": 4321,
|
||||
"Id": 12345
|
||||
},
|
||||
{
|
||||
"Nieprzeczytana": true,
|
||||
"Data": "2016-04-20 22:00:00",
|
||||
"Tresc": null,
|
||||
"Temat": "\"Dzień dobrego słowa\"",
|
||||
"NadawcaNazwa": "Pazura Agnieszka",
|
||||
"IdWiadomosci": 1235,
|
||||
"IdNadawca": 12,
|
||||
"Id": 12346
|
||||
},
|
||||
{
|
||||
"Nieprzeczytana": false,
|
||||
"Data": "2016-04-29 10:00:00",
|
||||
"Tresc": null,
|
||||
"Temat": "Rozdajemy oceny celujące",
|
||||
"NadawcaNazwa": "Kowalski Jan",
|
||||
"IdWiadomosci": 1236,
|
||||
"IdNadawca": 4321,
|
||||
"Id": 12347
|
||||
}
|
||||
]
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
{
|
||||
"success": true,
|
||||
"data": []
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
|
||||
|
||||
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||
<meta http-equiv="Content-Language" content="pl" />
|
||||
<meta name="robots" content="noindex" />
|
||||
<title>Błąd strony</title>
|
||||
<style type="text/css">
|
||||
|
||||
div.page
|
||||
{
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
text-align:center;
|
||||
}
|
||||
|
||||
div.pageHeader
|
||||
{
|
||||
width: 100%;
|
||||
height: 100px;
|
||||
background-color: transparent;
|
||||
background-position: center center;
|
||||
background-repeat: no-repeat;
|
||||
}
|
||||
|
||||
.pageContent
|
||||
{
|
||||
padding: 0px;
|
||||
margin-top: 100px;
|
||||
margin-bottom: 200px;
|
||||
clear: both;
|
||||
}
|
||||
|
||||
.pageFooter
|
||||
{
|
||||
|
||||
display: block;
|
||||
clear: both;
|
||||
border: solid #000 0px;
|
||||
text-align: center;
|
||||
margin-top: 20px;
|
||||
margin-bottom: 20px;
|
||||
padding: 20px;
|
||||
|
||||
color: #a1a1a1;
|
||||
font-size: 13px;
|
||||
text-align: center;
|
||||
background-color: transparent;
|
||||
|
||||
background-position: center top;
|
||||
background-repeat: no-repeat;
|
||||
}
|
||||
|
||||
.errorTitle
|
||||
{
|
||||
font-size: 16px;
|
||||
font-weight: bold;
|
||||
color: red;
|
||||
|
||||
}
|
||||
|
||||
.errorMessage
|
||||
{
|
||||
font-size: 15px;
|
||||
}
|
||||
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="page">
|
||||
<div class="pageHeader"> </div>
|
||||
<div class="pageContent">
|
||||
<div class="errorBlock">
|
||||
<span class="errorTitle">Wystąpił nieoczekiwany błąd</span>
|
||||
<div class="errorMessage">
|
||||
<pre>Wystąpił błąd aplikacji. Prosimy zalogować się ponownie. Jeśli problem będzie się powtarzał, prosimy o kontakt z serwisem.</pre>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="pageFooter">
|
||||
Copyright © VULCAN 2018 Wszelkie prawa zastrzeżone
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
@ -0,0 +1 @@
|
||||
The custom error module does not recognize this error.
|
Loading…
x
Reference in New Issue
Block a user