Bulk lookup implementation, part 1
- LDAP bulk lookup
This commit is contained in:
@@ -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.controller.v1;
|
||||
|
||||
import io.kamax.mxisd.lookup.ThreePidMapping;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
public class ClientBulkLookupAnswer {
|
||||
|
||||
private List<List<String>> threepids = new ArrayList<>();
|
||||
|
||||
public void addAll(Collection<ThreePidMapping> mappings) {
|
||||
for (ThreePidMapping mapping : mappings) {
|
||||
threepids.add(Arrays.asList(mapping.getMedium(), mapping.getValue(), mapping.getMxid()));
|
||||
}
|
||||
}
|
||||
|
||||
public List<List<String>> getThreepids() {
|
||||
return threepids;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.controller.v1
|
||||
|
||||
class ClientBulkLookupRequest {
|
||||
|
||||
private List<List<String>> threepids
|
||||
|
||||
List<List<String>> getThreepids() {
|
||||
return threepids
|
||||
}
|
||||
|
||||
void setThreepids(List<List<String>> threepids) {
|
||||
this.threepids = threepids
|
||||
}
|
||||
|
||||
}
|
||||
@@ -21,8 +21,11 @@
|
||||
package io.kamax.mxisd.controller.v1
|
||||
|
||||
import groovy.json.JsonOutput
|
||||
import groovy.json.JsonSlurper
|
||||
import io.kamax.mxisd.api.ThreePidType
|
||||
import io.kamax.mxisd.lookup.LookupRequest
|
||||
import io.kamax.mxisd.lookup.BulkLookupRequest
|
||||
import io.kamax.mxisd.lookup.SingleLookupRequest
|
||||
import io.kamax.mxisd.lookup.ThreePidMapping
|
||||
import io.kamax.mxisd.lookup.strategy.LookupStrategy
|
||||
import io.kamax.mxisd.signature.SignatureManager
|
||||
import org.apache.commons.lang.StringUtils
|
||||
@@ -36,11 +39,13 @@ import org.springframework.web.bind.annotation.RestController
|
||||
import javax.servlet.http.HttpServletRequest
|
||||
|
||||
import static org.springframework.web.bind.annotation.RequestMethod.GET
|
||||
import static org.springframework.web.bind.annotation.RequestMethod.POST
|
||||
|
||||
@RestController
|
||||
class MappingController {
|
||||
|
||||
private Logger log = LoggerFactory.getLogger(MappingController.class)
|
||||
private JsonSlurper json = new JsonSlurper()
|
||||
|
||||
@Autowired
|
||||
private LookupStrategy strategy
|
||||
@@ -55,7 +60,7 @@ class MappingController {
|
||||
|
||||
ThreePidType type = ThreePidType.valueOf(medium)
|
||||
|
||||
LookupRequest lookupRequest = new LookupRequest()
|
||||
SingleLookupRequest lookupRequest = new SingleLookupRequest()
|
||||
lookupRequest.setRequester(remote)
|
||||
lookupRequest.setType(type)
|
||||
lookupRequest.setThreePid(address)
|
||||
@@ -75,4 +80,27 @@ class MappingController {
|
||||
return JsonOutput.toJson(lookup)
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/_matrix/identity/api/v1/bulk_lookup", method = POST)
|
||||
String bulkLookup(HttpServletRequest request) {
|
||||
String remote = StringUtils.defaultIfBlank(request.getHeader("X-FORWARDED-FOR"), request.getRemoteAddr())
|
||||
log.info("Got request from {}", remote)
|
||||
|
||||
BulkLookupRequest lookupRequest = new BulkLookupRequest()
|
||||
lookupRequest.setRequester(remote)
|
||||
|
||||
ClientBulkLookupRequest input = (ClientBulkLookupRequest) json.parseText(request.getInputStream().getText())
|
||||
List<ThreePidMapping> mappings = new ArrayList<>()
|
||||
for (List<String> mappingRaw : input.getThreepids()) {
|
||||
ThreePidMapping mapping = new ThreePidMapping()
|
||||
mapping.setMedium(mappingRaw.get(0))
|
||||
mapping.setValue(mappingRaw.get(1))
|
||||
mappings.add(mapping)
|
||||
}
|
||||
lookupRequest.setMappings(mappings)
|
||||
|
||||
ClientBulkLookupAnswer answer = new ClientBulkLookupAnswer()
|
||||
answer.addAll(strategy.find(lookupRequest))
|
||||
return JsonOutput.toJson(answer)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user