NetTestWebService.java (apache-openmeetings-6.1.0-src) | : | NetTestWebService.java (apache-openmeetings-6.2.0-src) | ||
---|---|---|---|---|
skipping to change at line 43 | skipping to change at line 43 | |||
import javax.ws.rs.core.MediaType; | import javax.ws.rs.core.MediaType; | |||
import javax.ws.rs.core.Response; | import javax.ws.rs.core.Response; | |||
import javax.ws.rs.core.Response.Status; | import javax.ws.rs.core.Response.Status; | |||
import org.apache.openmeetings.webservice.util.RateLimited; | import org.apache.openmeetings.webservice.util.RateLimited; | |||
import org.slf4j.Logger; | import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | |||
import org.springframework.beans.factory.annotation.Value; | import org.springframework.beans.factory.annotation.Value; | |||
import org.springframework.stereotype.Service; | import org.springframework.stereotype.Service; | |||
import io.swagger.v3.oas.annotations.Operation; | ||||
import io.swagger.v3.oas.annotations.Parameter; | ||||
import io.swagger.v3.oas.annotations.media.Content; | ||||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||||
import io.swagger.v3.oas.annotations.tags.Tag; | ||||
/** | ||||
* | ||||
* The Service contains methods to trigger a network test | ||||
* | ||||
*/ | ||||
@Service("netTestWebService") | @Service("netTestWebService") | |||
@Tag(name = "NetTestService") | ||||
@Path("/networktest") | @Path("/networktest") | |||
public class NetTestWebService { | public class NetTestWebService { | |||
private static final Logger log = LoggerFactory.getLogger(NetTestWebServi ce.class); | private static final Logger log = LoggerFactory.getLogger(NetTestWebServi ce.class); | |||
public enum TestType { | public enum TestType { | |||
UNKNOWN, | UNKNOWN, | |||
PING, | PING, | |||
JITTER, | JITTER, | |||
DOWNLOAD_SPEED, | DOWNLOAD_SPEED, | |||
UPLOAD_SPEED | UPLOAD_SPEED | |||
} | } | |||
skipping to change at line 67 | skipping to change at line 79 | |||
private static final int MAX_DOWNLOAD_SIZE = 5 * 1024 * 1024; | private static final int MAX_DOWNLOAD_SIZE = 5 * 1024 * 1024; | |||
private static final int MAX_UPLOAD_SIZE = 5 * 512 * 1024; | private static final int MAX_UPLOAD_SIZE = 5 * 512 * 1024; | |||
public static final AtomicInteger CLIENT_COUNT = new AtomicInteger(); | public static final AtomicInteger CLIENT_COUNT = new AtomicInteger(); | |||
private static int maxClients = 100; | private static int maxClients = 100; | |||
@PostConstruct | @PostConstruct | |||
private void report() { | private void report() { | |||
log.debug("MaxClients: {}", maxClients); | log.debug("MaxClients: {}", maxClients); | |||
} | } | |||
/** | ||||
* Generate a sample for network test | ||||
* | ||||
* @param type one of ping, jitter, download, upload | ||||
* @param inSize requests size of sample | ||||
* @return Content as requested | ||||
*/ | ||||
@RateLimited | @RateLimited | |||
@GET | @GET | |||
@Produces(MediaType.APPLICATION_OCTET_STREAM) | @Produces(MediaType.APPLICATION_OCTET_STREAM) | |||
@Path("/") | @Path("/") | |||
public Response get(@QueryParam("type") String type, @QueryParam("size") | @Operation( | |||
int inSize) { | description = "Generate a sample for network test", | |||
responses = { | ||||
@ApiResponse(responseCode = "200", descri | ||||
ption = "Content as requested", content = @Content(mediaType = "application/octe | ||||
t-stream")), | ||||
@ApiResponse(responseCode = "400", descri | ||||
ption = "Unkown type or exceeding Max Upload size"), | ||||
@ApiResponse(responseCode = "500", descri | ||||
ption = "Server error") | ||||
} | ||||
) | ||||
public Response get( | ||||
@Parameter(required = true, description = "one of ping, j | ||||
itter, download, upload") @QueryParam("type") String type | ||||
, @Parameter(required = true, description = "requests siz | ||||
e of sample") @QueryParam("size") int inSize) { | ||||
final int size; | final int size; | |||
final TestType testType = getTypeByString(type); | final TestType testType = getTypeByString(type); | |||
log.debug("Network test:: get, {}, {}", testType, inSize); | log.debug("Network test:: get, {}, {}", testType, inSize); | |||
if (TestType.UNKNOWN == testType) { | if (TestType.UNKNOWN == testType) { | |||
return Response.status(Status.BAD_REQUEST).build(); | return Response.status(Status.BAD_REQUEST).build(); | |||
} | } | |||
// choose data to send | // choose data to send | |||
switch (testType) { | switch (testType) { | |||
case PING: | case PING: | |||
skipping to change at line 126 | skipping to change at line 155 | |||
} | } | |||
super.close(); | super.close(); | |||
} | } | |||
}) | }) | |||
.header("Cache-Control", "no-cache, no-store, no- transform") | .header("Cache-Control", "no-cache, no-store, no- transform") | |||
.header("Pragma", "no-cache") | .header("Pragma", "no-cache") | |||
.header("Content-Length", String.valueOf(size)) | .header("Content-Length", String.valueOf(size)) | |||
.build(); | .build(); | |||
} | } | |||
/** | ||||
* Upload media to test upload speed | ||||
* | ||||
* @param size size of file | ||||
* @param stream stream of file | ||||
* @return Number of bytes uploaded | ||||
* @throws IOException | ||||
*/ | ||||
@RateLimited | @RateLimited | |||
@POST | @POST | |||
@Consumes(MediaType.APPLICATION_OCTET_STREAM) | @Consumes(MediaType.APPLICATION_OCTET_STREAM) | |||
@Path("/") | @Path("/") | |||
public int upload(@QueryParam("size") int size, InputStream stream) throw | @Operation( | |||
s IOException { | description = "Upload media to test upload speed", | |||
responses = { | ||||
@ApiResponse(responseCode = "200", descri | ||||
ption = "Number of bytes uploaded"), | ||||
@ApiResponse(responseCode = "500", descri | ||||
ption = "Server error") | ||||
} | ||||
) | ||||
public int upload( | ||||
@Parameter(required = true, description = "size") @QueryP | ||||
aram("size") int size | ||||
, @Parameter(required = true, description = "stream to up | ||||
load") InputStream stream) throws IOException { | ||||
if (size > MAX_UPLOAD_SIZE) { | if (size > MAX_UPLOAD_SIZE) { | |||
return -1; | return -1; | |||
} | } | |||
CLIENT_COUNT.incrementAndGet(); | CLIENT_COUNT.incrementAndGet(); | |||
byte[] b = new byte[1024]; | byte[] b = new byte[1024]; | |||
int totalCount = 0; | int totalCount = 0; | |||
int count; | int count; | |||
try { | try { | |||
while ((count = stream.read(b)) > -1) { | while ((count = stream.read(b)) > -1) { | |||
totalCount += count; | totalCount += count; | |||
End of changes. 6 change blocks. | ||||
4 lines changed or deleted | 58 lines changed or added |