Auth endpoint implementation
This commit is contained in:
73
src/main/groovy/io/kamax/mxisd/util/GsonParser.java
Normal file
73
src/main/groovy/io/kamax/mxisd/util/GsonParser.java
Normal file
@@ -0,0 +1,73 @@
|
||||
/*
|
||||
* mxisd - Matrix Identity Server Daemon
|
||||
* Copyright (C) 2017 Maxime Dor
|
||||
*
|
||||
* https://max.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.mxisd.util;
|
||||
|
||||
import com.google.gson.*;
|
||||
import io.kamax.mxisd.exception.InvalidResponseJsonException;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.HttpResponse;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class GsonParser {
|
||||
|
||||
private JsonParser parser = new JsonParser();
|
||||
private Gson gson;
|
||||
|
||||
public GsonParser() {
|
||||
this(new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create());
|
||||
}
|
||||
|
||||
public GsonParser(Gson gson) {
|
||||
this.gson = gson;
|
||||
}
|
||||
|
||||
public JsonObject parse(InputStream stream, String property) throws IOException {
|
||||
JsonElement el = parser.parse(IOUtils.toString(stream, StandardCharsets.UTF_8));
|
||||
if (!el.isJsonObject()) {
|
||||
throw new InvalidResponseJsonException("Response body is not a JSON object");
|
||||
}
|
||||
|
||||
JsonObject obj = el.getAsJsonObject();
|
||||
if (!obj.has(property)) {
|
||||
throw new IOException("Member " + property + " does not exist");
|
||||
}
|
||||
|
||||
el = obj.get(property);
|
||||
if (!el.isJsonObject()) {
|
||||
throw new InvalidResponseJsonException("Member " + property + " is not a JSON object");
|
||||
}
|
||||
|
||||
return el.getAsJsonObject();
|
||||
}
|
||||
|
||||
public <T> T parse(InputStream stream, String memberName, Class<T> type) throws IOException {
|
||||
JsonObject obj = parse(stream, memberName);
|
||||
return gson.fromJson(obj, type);
|
||||
}
|
||||
|
||||
public <T> T parse(HttpResponse res, String memberName, Class<T> type) throws IOException {
|
||||
return parse(res.getEntity().getContent(), memberName, type);
|
||||
}
|
||||
|
||||
}
|
||||
38
src/main/groovy/io/kamax/mxisd/util/JsonUtils.java
Normal file
38
src/main/groovy/io/kamax/mxisd/util/JsonUtils.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* mxisd - Matrix Identity Server Daemon
|
||||
* Copyright (C) 2017 Maxime Dor
|
||||
*
|
||||
* https://max.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.mxisd.util;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
public class JsonUtils {
|
||||
|
||||
public static JsonObject getObj(Gson gson, String property, Object value) {
|
||||
JsonObject obj = new JsonObject();
|
||||
obj.add(property, gson.toJsonTree(value));
|
||||
return obj;
|
||||
}
|
||||
|
||||
public static String getObjAsString(Gson gson, String property, Object value) {
|
||||
return gson.toJson(getObj(gson, property, value));
|
||||
}
|
||||
|
||||
}
|
||||
44
src/main/groovy/io/kamax/mxisd/util/RestClientUtils.java
Normal file
44
src/main/groovy/io/kamax/mxisd/util/RestClientUtils.java
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* mxisd - Matrix Identity Server Daemon
|
||||
* Copyright (C) 2017 Maxime Dor
|
||||
*
|
||||
* https://max.kamax.io/
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package io.kamax.mxisd.util;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.entity.StringEntity;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class RestClientUtils {
|
||||
|
||||
public static HttpPost post(String url, String body) {
|
||||
StringEntity entity = new StringEntity(body, StandardCharsets.UTF_8);
|
||||
entity.setContentType(ContentType.APPLICATION_JSON.toString());
|
||||
HttpPost req = new HttpPost(url);
|
||||
req.setEntity(entity);
|
||||
return req;
|
||||
}
|
||||
|
||||
public static HttpPost post(String url, Gson gson, String member, Object o) {
|
||||
return post(url, JsonUtils.getObjAsString(gson, member, o));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user