Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create multiple chat rooms in one request #134

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.xmpp.packet.Presence;

import javax.annotation.Nonnull;
import javax.servlet.ServletException;
import javax.ws.rs.core.Response;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
Expand Down Expand Up @@ -258,6 +257,41 @@ public void createChatRoom(String serviceName, MUCRoomEntity mucRoomEntity) thro
}
}

/**
* Creates multiple chat rooms.
*
* @param serviceName
* the service name
* @param mucRoomEntities
* the chat rooms to create
* @return
* a report detailing which creates were successful and which weren't
* @throws ServiceException
* the service exception
*/
public RoomCreationResultEntities createMultipleChatRooms(String serviceName, MUCRoomEntities mucRoomEntities) throws ServiceException {
List<MUCRoomEntity> roomsToCreate = mucRoomEntities.getMucRooms();
log("Create " + roomsToCreate.size() + " chat rooms");
List<RoomCreationResultEntity> results = new ArrayList<>();
for (MUCRoomEntity roomToCreate : roomsToCreate) {
RoomCreationResultEntity result = new RoomCreationResultEntity();
result.setRoomName(roomToCreate.getRoomName());
try {
createRoom(roomToCreate, serviceName);
result.setResultType(RoomCreationResultEntity.RoomCreationResultType.Success);
result.setMessage("Room was successfully created");
} catch (AlreadyExistsException e) {
result.setResultType(RoomCreationResultEntity.RoomCreationResultType.Success);
result.setMessage("Room already existed and therefore not created again");
} catch (NotAllowedException | ForbiddenException | ConflictException e) {
result.setResultType(RoomCreationResultEntity.RoomCreationResultType.Failure);
result.setMessage("Room creation failed due to " + e.getClass().getSimpleName() + ": " + e.getMessage());
}
results.add(result);
}
return new RoomCreationResultEntities(results);
}

/**
* Update chat room.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2022.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jivesoftware.openfire.plugin.rest.entity;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;

@XmlRootElement(name = "results")
public class RoomCreationResultEntities {
List<RoomCreationResultEntity> results;

public RoomCreationResultEntities() {
}

public RoomCreationResultEntities(List<RoomCreationResultEntity> results) {
this.results = results;
}

@XmlElement(name = "result")
public List<RoomCreationResultEntity> getResults() {
return results;
}

public void setResults(List<RoomCreationResultEntity> results) {
this.results = results;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (c) 2022.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jivesoftware.openfire.plugin.rest.entity;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


@XmlRootElement(name = "result")
@XmlType(propOrder = { "roomName", "resultType", "message"})
public class RoomCreationResultEntity {

public enum RoomCreationResultType {
Success, Failure
}

String roomName;
RoomCreationResultType resultType;
String message;

@XmlElement
public String getRoomName() {
return roomName;
}

public void setRoomName(String roomName) {
this.roomName = roomName;
}

@XmlElement
public RoomCreationResultType getResultType() {
return resultType;
}

public void setResultType(RoomCreationResultType resultType) {
this.resultType = resultType;
}

@XmlElement
public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ public Response createMUCRoom(
return Response.status(Status.CREATED).build();
}

@POST
@Path("/bulk")
@Operation( summary = "Create multiple chat rooms",
description = "Create a number of new multi-user chat rooms.",
responses = {
@ApiResponse(responseCode = "200", description = "Request has been processed. Results are reported in the response.", content = @Content(schema = @Schema(implementation = RoomCreationResultEntities.class))),
@ApiResponse(responseCode = "401", description = "Web service authentication failed.", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "404", description = "MUC Service does not exist or is not accessible.", content = @Content(schema = @Schema(implementation = ErrorResponse.class))),
@ApiResponse(responseCode = "500", description = "Unexpected, generic error condition.", content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public RoomCreationResultEntities createMUCRooms(
@Parameter(description = "The name of the MUC service in which to create a chat room.", example = "conference", required = false) @DefaultValue("conference") @QueryParam("servicename") String serviceName,
@RequestBody(description = "The MUC rooms that need to be created.", required = true) MUCRoomEntities mucRoomEntities)
throws ServiceException
{
return MUCRoomController.getInstance().createMultipleChatRooms(serviceName, mucRoomEntities);
}

@PUT
@Path("/{roomName}")
@Operation( summary = "Update chat room",
Expand Down