Search in sources :

Example 61 with TypeToken

use of com.google.gson.reflect.TypeToken in project incubator-pulsar by apache.

the class ProxyPublishConsumeTest method verifyProxyMetrics.

private void verifyProxyMetrics(Client client, String baseUrl) {
    // generate metrics
    service.getProxyStats().generate();
    // collect metrics
    String statUrl = baseUrl + "metrics";
    WebTarget webTarget = client.target(statUrl);
    Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
    Response response = (Response) invocationBuilder.get();
    String responseStr = response.readEntity(String.class);
    final Gson gson = new Gson();
    final List<Metrics> data = gson.fromJson(responseStr, new TypeToken<List<Metrics>>() {
    }.getType());
    Assert.assertFalse(data.isEmpty());
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(javax.ws.rs.core.Response) Metrics(org.apache.pulsar.common.stats.Metrics) Invocation(javax.ws.rs.client.Invocation) TypeToken(com.google.gson.reflect.TypeToken) Gson(com.google.gson.Gson) WebTarget(javax.ws.rs.client.WebTarget)

Example 62 with TypeToken

use of com.google.gson.reflect.TypeToken in project opencast by opencast.

the class WowzaAdaptiveStreamingDistributionRestService method retract.

@POST
@Path("/retract")
@Produces(MediaType.TEXT_XML)
@RestQuery(name = "retract", description = "Retract a media package element from this distribution channel", returnDescription = "The job that can be used to track the retraction", restParameters = { @RestParameter(name = "mediapackage", isRequired = true, description = "The mediapackage", type = Type.TEXT), @RestParameter(name = "channelId", isRequired = true, description = "The publication channel ID", type = Type.TEXT), @RestParameter(name = "elementIds", isRequired = true, description = "The elements to retract as Json Array['IdOne','IdTwo']", type = Type.STRING) }, reponses = { @RestResponse(responseCode = SC_OK, description = "An XML representation of the retraction job"), @RestResponse(responseCode = SC_NO_CONTENT, description = "There is no streaming distribution service available") })
public Response retract(@FormParam("mediapackage") String mediaPackageXml, @FormParam("channelId") String channelId, @FormParam("elementIds") String elementIds) throws Exception {
    Job job = null;
    try {
        Set<String> setElementIds = gson.fromJson(elementIds, new TypeToken<Set<String>>() {
        }.getType());
        MediaPackage mediapackage = MediaPackageParser.getFromXml(mediaPackageXml);
        job = service.retract(channelId, mediapackage, setElementIds);
        if (job == null) {
            return Response.noContent().build();
        } else {
            return Response.ok(new JaxbJob(job)).build();
        }
    } catch (IllegalArgumentException e) {
        logger.debug("Unable to retract element: {}", e.getMessage());
        return status(Status.BAD_REQUEST).build();
    } catch (Exception e) {
        logger.warn("Unable to retract mediapackage '{}' from streaming channel: {}", new Object[] { mediaPackageXml, e });
        return Response.serverError().status(Status.INTERNAL_SERVER_ERROR).build();
    }
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) JaxbJob(org.opencastproject.job.api.JaxbJob) MediaPackage(org.opencastproject.mediapackage.MediaPackage) JaxbJob(org.opencastproject.job.api.JaxbJob) Job(org.opencastproject.job.api.Job) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 63 with TypeToken

use of com.google.gson.reflect.TypeToken in project opencast by opencast.

the class AwsS3DistributionServiceImpl method process.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.job.api.AbstractJobProducer#process(org.opencastproject.job.api.Job)
 */
@Override
protected String process(Job job) throws Exception {
    Operation op = null;
    String operation = job.getOperation();
    List<String> arguments = job.getArguments();
    try {
        op = Operation.valueOf(operation);
        String channelId = arguments.get(0);
        MediaPackage mediaPackage = MediaPackageParser.getFromXml(arguments.get(1));
        Set<String> elementIds = gson.fromJson(arguments.get(2), new TypeToken<Set<String>>() {
        }.getType());
        switch(op) {
            case Distribute:
                Boolean checkAvailability = Boolean.parseBoolean(arguments.get(3));
                MediaPackageElement[] distributedElements = distributeElements(channelId, mediaPackage, elementIds, checkAvailability);
                return (distributedElements != null) ? MediaPackageElementParser.getArrayAsXml(Arrays.asList(distributedElements)) : null;
            case Retract:
                MediaPackageElement[] retractedElements = retractElements(channelId, mediaPackage, elementIds);
                return (retractedElements != null) ? MediaPackageElementParser.getArrayAsXml(Arrays.asList(retractedElements)) : null;
            /*
         * TODO
         * Commented out due to changes in the way the element IDs are passed (ie, a list rather than individual ones
         * per job). This code is still useful long term, but I don't have time to write the necessary wrapper code
         * around it right now.
         * case Restore:
         * String fileName = arguments.get(3);
         * MediaPackageElement restoredElement = null;
         * if (StringUtils.isNotBlank(fileName)) {
         * restoredElement = restoreElement(channelId, mediaPackage, elementIds, fileName);
         * } else {
         * restoredElement = restoreElement(channelId, mediaPackage, elementIds, null);
         * }
         * return (restoredElement != null) ? MediaPackageElementParser.getAsXml(restoredElement) : null;
         */
            default:
                throw new IllegalStateException("Don't know how to handle operation '" + operation + "'");
        }
    } catch (IllegalArgumentException e) {
        throw new ServiceRegistryException("This service can't handle operations of type '" + op + "'", e);
    } catch (IndexOutOfBoundsException e) {
        throw new ServiceRegistryException("This argument list for operation '" + op + "' does not meet expectations", e);
    } catch (Exception e) {
        throw new ServiceRegistryException("Error handling operation '" + op + "'", e);
    }
}
Also used : ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) URISyntaxException(java.net.URISyntaxException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) AmazonServiceException(com.amazonaws.AmazonServiceException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) AmazonClientException(com.amazonaws.AmazonClientException) DistributionException(org.opencastproject.distribution.api.DistributionException) NotFoundException(org.opencastproject.util.NotFoundException) ConfigurationException(org.opencastproject.util.ConfigurationException) IOException(java.io.IOException) TypeToken(com.google.gson.reflect.TypeToken) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage)

Example 64 with TypeToken

use of com.google.gson.reflect.TypeToken in project opencast by opencast.

the class DownloadDistributionServiceImpl method process.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.job.api.AbstractJobProducer#process(org.opencastproject.job.api.Job)
 */
@Override
protected String process(Job job) throws Exception {
    Operation op = null;
    String operation = job.getOperation();
    List<String> arguments = job.getArguments();
    try {
        op = Operation.valueOf(operation);
        String channelId = arguments.get(0);
        MediaPackage mediapackage = MediaPackageParser.getFromXml(arguments.get(1));
        Set<String> elementIds = gson.fromJson(arguments.get(2), new TypeToken<Set<String>>() {
        }.getType());
        switch(op) {
            case Distribute:
                Boolean checkAvailability = Boolean.parseBoolean(arguments.get(3));
                Boolean preserveReference = Boolean.parseBoolean(arguments.get(4));
                MediaPackageElement[] distributedElements = distributeElements(channelId, mediapackage, elementIds, checkAvailability, preserveReference);
                return (distributedElements != null) ? MediaPackageElementParser.getArrayAsXml(Arrays.asList(distributedElements)) : null;
            case Retract:
                MediaPackageElement[] retractedElements = retractElements(channelId, mediapackage, elementIds);
                return (retractedElements != null) ? MediaPackageElementParser.getArrayAsXml(Arrays.asList(retractedElements)) : null;
            default:
                throw new IllegalStateException("Don't know how to handle operation '" + operation + "'");
        }
    } catch (IllegalArgumentException e) {
        throw new ServiceRegistryException("This service can't handle operations of type '" + op + "'", e);
    } catch (IndexOutOfBoundsException e) {
        throw new ServiceRegistryException("This argument list for operation '" + op + "' does not meet expectations", e);
    } catch (Exception e) {
        throw new ServiceRegistryException("Error handling operation '" + op + "'", e);
    }
}
Also used : ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) URISyntaxException(java.net.URISyntaxException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) DistributionException(org.opencastproject.distribution.api.DistributionException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) TypeToken(com.google.gson.reflect.TypeToken) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage)

Example 65 with TypeToken

use of com.google.gson.reflect.TypeToken in project opencast by opencast.

the class StreamingDistributionServiceImpl method process.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.job.api.AbstractJobProducer#process(org.opencastproject.job.api.Job)
 */
@Override
protected String process(Job job) throws Exception {
    Operation op = null;
    String operation = job.getOperation();
    List<String> arguments = job.getArguments();
    try {
        op = Operation.valueOf(operation);
        String channelId = arguments.get(0);
        MediaPackage mediapackage = MediaPackageParser.getFromXml(arguments.get(1));
        Set<String> elementIds = gson.fromJson(arguments.get(2), new TypeToken<Set<String>>() {
        }.getType());
        switch(op) {
            case Distribute:
                MediaPackageElement[] distributedElements = distributeElements(channelId, mediapackage, elementIds);
                return (distributedElements != null) ? MediaPackageElementParser.getArrayAsXml(Arrays.asList(distributedElements)) : null;
            case Retract:
                MediaPackageElement[] retractedElements = retractElements(channelId, mediapackage, elementIds);
                return (retractedElements != null) ? MediaPackageElementParser.getArrayAsXml(Arrays.asList(retractedElements)) : null;
            default:
                throw new IllegalStateException("Don't know how to handle operation '" + operation + "'");
        }
    } catch (IllegalArgumentException e) {
        throw new ServiceRegistryException("This service can't handle operations of type '" + op + "'", e);
    } catch (IndexOutOfBoundsException e) {
        throw new ServiceRegistryException("This argument list for operation '" + op + "' does not meet expectations", e);
    } catch (Exception e) {
        throw new ServiceRegistryException("Error handling operation '" + op + "'", e);
    }
}
Also used : ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) URISyntaxException(java.net.URISyntaxException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ServiceRegistryException(org.opencastproject.serviceregistry.api.ServiceRegistryException) MediaPackageException(org.opencastproject.mediapackage.MediaPackageException) DistributionException(org.opencastproject.distribution.api.DistributionException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException) TypeToken(com.google.gson.reflect.TypeToken) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage)

Aggregations

TypeToken (com.google.gson.reflect.TypeToken)418 Gson (com.google.gson.Gson)178 Test (org.junit.Test)99 IOException (java.io.IOException)83 Map (java.util.Map)71 List (java.util.List)56 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)54 ArrayList (java.util.ArrayList)53 HashMap (java.util.HashMap)52 GsonBuilder (com.google.gson.GsonBuilder)45 File (java.io.File)34 Notebook (org.apache.zeppelin.notebook.Notebook)32 Type (java.lang.reflect.Type)31 FileNotFoundException (java.io.FileNotFoundException)29 Paragraph (org.apache.zeppelin.notebook.Paragraph)27 RestResponse (com.google.gerrit.acceptance.RestResponse)24 JsonElement (com.google.gson.JsonElement)24 JsonObject (com.google.gson.JsonObject)24 OutputStreamWriter (java.io.OutputStreamWriter)22 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)21