igniterealtime/REST-API-Client

How can I delete a user from a group?

Closed this issue · 8 comments

I didn't see api within RestApiClient class.

Redor commented

Yes, you are right, it is not yet possible to remove a user from a group. That have an limitation of jersey framework which I use to send request.
I need at first need a change on the REST API from Openfire to make it possible.

Why upgrade jersey framework from 1.x to 2.21? There are a lot things implemented and some new providers like genson.

Redor commented

Do you know if its possible to have a payload by an DELETE request, in jersey 2?

Do you mean the code like below? I have implement it with jersey 2, for now, it works except receive xml format exception. That is because the rest-api does not have JSON ExceptionMapper.

baseURI = "https://192.168.56.102:9091/plugins/restapi/v1/";
WebTarget target = client.target(getBaseURI());
Invocation invocation = requestBuilder.accept(MediaType.APPLICATION_JSON).buildDelete();
invocation.invoke();
...
ClientConfiguration contains definitions like timeout.
private static Client buildClient(ClientConfiguration clientConfiguration) {
ClientConfig clientConfig = new ClientConfig();
ConnectorProvider connectorProvider = new ApacheConnectorProvider();
clientConfig.connectorProvider(connectorProvider);
clientConfig.property(ClientProperties.CONNECT_TIMEOUT, clientConfiguration.getConnectTimeoutMillis());
clientConfig.property(ClientProperties.READ_TIMEOUT, clientConfiguration.getReadTimeoutMillis());
Client builtClient = null;
ConnectionSocketFactory connectionSocketFactory;
String urlPrefix;
//config your ssl for apache connector
if (clientConfiguration.useSSL()) {
urlPrefix = "https";
TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}};
SSLContext sc = null;
try {
sc = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
sc.init(null, trustAllCerts, new SecureRandom());
} catch (Exception e) {
throw new IllegalArgumentException("Illegal SSL instance for TLS", e);
}
connectionSocketFactory = new SSLConnectionSocketFactory(sc, new HostnameVerifier() {
@OverRide
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
} else {
urlPrefix = "http";
connectionSocketFactory = PlainConnectionSocketFactory.getSocketFactory();
}
RegistryBuilder registryBuilder = RegistryBuilder.create();
registryBuilder.register(urlPrefix, connectionSocketFactory);
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registryBuilder.build());
clientConfig.property(ApacheClientProperties.CONNECTION_MANAGER, connectionManager);
if (clientConfiguration.useBasicAuth()) {
HttpAuthenticationFeature httpAuthenticationFeature = HttpAuthenticationFeature.universalBuilder()
.credentialsForBasic(clientConfiguration.getAdminUsername(), clientConfiguration.getAdminPassword())
.build();
builtClient.register(httpAuthenticationFeature);
}
clientConfig.register(new GZipEncoder());
clientConfig.register(Slf4jLoggingFilter.builder().logger(logger).build());
clientConfig.register(GensonJsonProvider.class);
clientConfig.register(new GensonJaxRSFeature().use(JsonUtil.getGenson()));
builtClient = ClientBuilder.newBuilder().withConfig(clientConfig).build();
return builtClient;
}

Redor commented

FYI: I already fixed it in the REST API. (http://www.igniterealtime.org/projects/openfire/plugins/restapi/readme.html#delete-a-user-from-a-group)
And will implement it today to the client too.

Hi, does client implement this yet?

Redor commented

Hey, sorry for the delay, i just released a new version 1.1.2 which contains this feature.

  // Add user to group
  restApiClient.addUserToGroup("testUsername", "Moderators");

  // Delete user from a group
  restApiClient.deleteUserFromGroup("testUsername", "Moderators");

👍