Search in sources :

Example 6 with MappingJsonFactory

use of org.codehaus.jackson.map.MappingJsonFactory in project onebusaway-application-modules by camsys.

the class ValidateResource method validate.

@Path("/{bundleDirectory}/{bundleName}/create")
@GET
@Produces("application/json")
public Response validate(@PathParam("bundleDirectory") String bundleDirectory, @PathParam("bundleName") String bundleName) {
    Response response = null;
    if (!isAuthorized()) {
        return Response.noContent().build();
    }
    BundleRequest bundleRequest = new BundleRequest();
    directoryName = bundleDirectory;
    this.bundleName = bundleName;
    bundleRequest.setBundleBuildName(bundleName);
    bundleRequest.setBundleDirectory(bundleDirectory);
    String session = RequestContextHolder.currentRequestAttributes().getSessionId();
    bundleRequest.setSessionId(session);
    BundleResponse bundleResponse = null;
    try {
        bundleResponse = _bundleService.validate(bundleRequest);
        final StringWriter sw = new StringWriter();
        final MappingJsonFactory jsonFactory = new MappingJsonFactory();
        final JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(sw);
        _mapper.writeValue(jsonGenerator, bundleResponse);
        response = Response.ok(sw.toString()).build();
    } catch (Exception any) {
        _log.error("validate resource caught exception:" + any);
        response = Response.serverError().build();
    }
    return response;
}
Also used : BundleResponse(org.onebusaway.admin.model.BundleResponse) Response(javax.ws.rs.core.Response) MappingJsonFactory(org.codehaus.jackson.map.MappingJsonFactory) BundleResponse(org.onebusaway.admin.model.BundleResponse) StringWriter(java.io.StringWriter) BundleRequest(org.onebusaway.admin.model.BundleRequest) JsonGenerator(org.codehaus.jackson.JsonGenerator) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 7 with MappingJsonFactory

use of org.codehaus.jackson.map.MappingJsonFactory in project onebusaway-application-modules by camsys.

the class BuildRemoteResource method build.

@Path("/create")
@POST
@Produces("application/json")
public Response build(@FormParam("bundleDirectory") String bundleDirectory, @FormParam("bundleBuildName") String bundleName, @FormParam("email") String email, @FormParam("id") String id, @FormParam("bundleStartDate") String bundleStartDate, @FormParam("bundleEndDate") String bundleEndDate, @FormParam("bundleComment") String bundleComment, @FormParam("archive") boolean archive, @FormParam("consolidate") boolean consolidate, @FormParam("predate") boolean predate) {
    Response response = null;
    if (!isAuthorized()) {
        return Response.noContent().build();
    }
    _log.info("in build(local) with archive=" + archive + ", and consolidate=" + consolidate);
    BundleBuildRequest bundleRequest = new BundleBuildRequest();
    bundleRequest.setBundleDirectory(bundleDirectory);
    bundleRequest.setBundleName(bundleName);
    bundleRequest.setEmailAddress(email);
    bundleRequest.setId(id);
    bundleRequest.setBundleStartDate(bundleStartDate);
    bundleRequest.setBundleEndDate(bundleEndDate);
    bundleRequest.setBundleComment(bundleComment);
    bundleRequest.setArchiveFlag(archive);
    bundleRequest.setConsolidateFlag(consolidate);
    bundleRequest.setPredate(predate);
    BundleBuildResponse bundleResponse = new BundleBuildResponse(id);
    try {
        bundleResponse.addStatusMessage("server started");
        bundleResponse.addStatusMessage("queueing");
        // place execution in its own thread
        _executorService.execute(new BuildThread(bundleRequest, bundleResponse));
        // place handle to response in map
        _buildMap.put(id, bundleResponse);
        final StringWriter sw = new StringWriter();
        final MappingJsonFactory jsonFactory = new MappingJsonFactory();
        final JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(sw);
        // write back response
        _mapper.writeValue(jsonGenerator, bundleResponse);
        response = Response.ok(sw.toString()).build();
    } catch (Exception any) {
        _log.error("execption in build:", any);
        response = Response.serverError().build();
    }
    return response;
}
Also used : BundleBuildResponse(org.onebusaway.admin.model.BundleBuildResponse) Response(javax.ws.rs.core.Response) BundleBuildRequest(org.onebusaway.admin.model.BundleBuildRequest) MappingJsonFactory(org.codehaus.jackson.map.MappingJsonFactory) StringWriter(java.io.StringWriter) JsonGenerator(org.codehaus.jackson.JsonGenerator) BundleBuildResponse(org.onebusaway.admin.model.BundleBuildResponse) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 8 with MappingJsonFactory

use of org.codehaus.jackson.map.MappingJsonFactory in project onebusaway-application-modules by camsys.

the class AgencyMetadataResource method constructResponse.

// Private methods
private Response constructResponse(Object result) {
    final StringWriter sw = new StringWriter();
    final MappingJsonFactory jsonFactory = new MappingJsonFactory();
    Response response = null;
    try {
        final org.codehaus.jackson.JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(sw);
        mapper.writeValue(jsonGenerator, result);
        response = Response.ok(sw.toString()).build();
    } catch (JsonGenerationException e) {
        log.error("Error generating response JSON");
        response = Response.serverError().build();
        e.printStackTrace();
    } catch (JsonMappingException e) {
        log.error("Error mapping response to JSON");
        response = Response.serverError().build();
        e.printStackTrace();
    } catch (IOException e) {
        log.error("I/O error while creating response JSON");
        response = Response.serverError().build();
        e.printStackTrace();
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) MappingJsonFactory(org.codehaus.jackson.map.MappingJsonFactory) StringWriter(java.io.StringWriter) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException)

Example 9 with MappingJsonFactory

use of org.codehaus.jackson.map.MappingJsonFactory in project onebusaway-application-modules by camsys.

the class KeysResource method constructResponse.

private Response constructResponse(Object result) {
    final StringWriter sw = new StringWriter();
    final MappingJsonFactory jsonFactory = new MappingJsonFactory();
    Response response = null;
    try {
        final JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(sw);
        mapper.writeValue(jsonGenerator, result);
        response = Response.ok(sw.toString()).build();
    } catch (JsonGenerationException e) {
        log.error("Error generating response JSON");
        response = Response.serverError().build();
        e.printStackTrace();
    } catch (JsonMappingException e) {
        log.error("Error mapping response to JSON");
        response = Response.serverError().build();
        e.printStackTrace();
    } catch (IOException e) {
        log.error("I/O error while creating response JSON");
        response = Response.serverError().build();
        e.printStackTrace();
    }
    return response;
}
Also used : Response(javax.ws.rs.core.Response) MappingJsonFactory(org.codehaus.jackson.map.MappingJsonFactory) StringWriter(java.io.StringWriter) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) JsonGenerator(org.codehaus.jackson.JsonGenerator) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException)

Example 10 with MappingJsonFactory

use of org.codehaus.jackson.map.MappingJsonFactory in project onebusaway-application-modules by camsys.

the class BundleServerServiceImpl method toJson.

private String toJson(Object payload) {
    String jsonPayload = null;
    if (payload != null) {
        StringWriter sw = new StringWriter();
        try {
            final MappingJsonFactory jsonFactory = new MappingJsonFactory();
            final JsonGenerator jsonGenerator = jsonFactory.createJsonGenerator(sw);
            _mapper.writeValue(jsonGenerator, payload);
        } catch (Exception any) {
            _log.error("json execption=", any);
        }
        jsonPayload = sw.toString();
    }
    return jsonPayload;
}
Also used : MappingJsonFactory(org.codehaus.jackson.map.MappingJsonFactory) StringWriter(java.io.StringWriter) JsonGenerator(org.codehaus.jackson.JsonGenerator) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException)

Aggregations

StringWriter (java.io.StringWriter)14 MappingJsonFactory (org.codehaus.jackson.map.MappingJsonFactory)14 JsonGenerator (org.codehaus.jackson.JsonGenerator)13 Response (javax.ws.rs.core.Response)10 Path (javax.ws.rs.Path)7 Produces (javax.ws.rs.Produces)7 GET (javax.ws.rs.GET)6 IOException (java.io.IOException)5 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)5 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)4 BundleResponse (org.onebusaway.admin.model.BundleResponse)4 BundleBuildResponse (org.onebusaway.admin.model.BundleBuildResponse)3 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)2 BundleRequest (org.onebusaway.admin.model.BundleRequest)2 POST (javax.ws.rs.POST)1 JsonParseException (org.codehaus.jackson.JsonParseException)1 BundleBuildRequest (org.onebusaway.admin.model.BundleBuildRequest)1 DateValidationException (org.onebusaway.admin.service.exceptions.DateValidationException)1