Example usage for javax.servlet.http HttpServletResponse setContentType

List of usage examples for javax.servlet.http HttpServletResponse setContentType

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse setContentType.

Prototype


public void setContentType(String type);

Source Link

Document

Sets the content type of the response being sent to the client, if the response has not been committed yet.

Usage

From source file:org.apache.cxf.fediz.service.idp.MetadataServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/xml; charset=utf-8");
    PrintWriter out = response.getWriter();
    try {/*www.ja  va2 s  .  c  o  m*/
        ConfigService cs = (ConfigService) getApplicationContext().getBean("config");
        Idp idpConfig = cs.getIDP(realm);
        LOG.debug(idpConfig.toString());
        MetadataWriter mw = new MetadataWriter();
        Document metadata = mw.getMetaData(idpConfig);
        out.write(DOM2Writer.nodeToString(metadata));
    } catch (Exception ex) {
        LOG.error("Failed to get metadata document: ", ex);
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}

From source file:org.mitre.openid.connect.view.JsonErrorView.java

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) {

    response.setContentType(MediaType.APPLICATION_JSON_VALUE);

    HttpStatus code = (HttpStatus) model.get(HttpCodeView.CODE);
    if (code == null) {
        code = HttpStatus.INTERNAL_SERVER_ERROR; // default to 500
    }//  w ww .  ja  v a2  s  .  c o  m

    response.setStatus(code.value());

    try {

        Writer out = response.getWriter();

        String errorTitle = (String) model.get(ERROR);
        if (Strings.isNullOrEmpty(errorTitle)) {
            errorTitle = "mitreid_error";
        }
        String errorMessage = (String) model.get(ERROR_MESSAGE);
        JsonObject obj = new JsonObject();
        obj.addProperty("error", errorTitle);
        obj.addProperty("error_description", errorMessage);
        gson.toJson(obj, out);

    } catch (IOException e) {

        logger.error("IOException in JsonErrorView.java: ", e);

    }
}

From source file:org.mitre.uma.view.ResourceSetEntityAbbreviatedView.java

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) {

    response.setContentType("application/json");

    HttpStatus code = (HttpStatus) model.get(HttpCodeView.CODE);
    if (code == null) {
        code = HttpStatus.OK; // default to 200
    }//from   w ww  . j  a  va  2s.com

    response.setStatus(code.value());

    String location = (String) model.get(LOCATION);
    if (!Strings.isNullOrEmpty(location)) {
        response.setHeader(HttpHeaders.LOCATION, location);
    }

    try {

        Writer out = response.getWriter();
        ResourceSet rs = (ResourceSet) model.get(JsonEntityView.ENTITY);

        JsonObject o = new JsonObject();

        o.addProperty("_id", rs.getId().toString()); // set the ID to a string
        o.addProperty("user_access_policy_uri", config.getIssuer() + "manage/user/policy/" + rs.getId());

        gson.toJson(o, out);

    } catch (IOException e) {

        logger.error("IOException in ResourceSetEntityView.java: ", e);

    }
}

From source file:com.aistor.modules.cms.web.CategoryController.java

@RequiresUser
@ResponseBody//from   w w w  . j  a va 2s  . co m
@RequestMapping(value = "treeData")
public String treeData(String module, @RequestParam(required = false) Long extId,
        HttpServletResponse response) {
    response.setContentType("application/json; charset=UTF-8");
    List<Map<String, Object>> mapList = Lists.newArrayList();
    List<Category> list = Lists.newArrayList();
    if (StringUtils.isNotBlank(module)) {
        list = categoryService.findByUserAndModule(module);
    } else {
        list = categoryService.findByUser(true);
    }
    for (int i = 0; i < list.size(); i++) {
        Category e = list.get(i);
        if (extId == null || (extId != null && !extId.equals(e.getId())
                && e.getParentIds().indexOf("," + extId + ",") == -1)) {
            Map<String, Object> map = Maps.newHashMap();
            map.put("id", e.getId());
            map.put("pId", e.getParent() != null ? e.getParent().getId() : 0);
            map.put("name", e.getName());
            map.put("module", e.getModule());
            mapList.add(map);
        }
    }
    return JsonMapper.getInstance().toJson(mapList);
}

From source file:com.lixiaocong.rest.FileController.java

@RequestMapping(value = "/imagecode")
public void imagecode(HttpSession session, HttpServletResponse response) throws IOException {
    response.setContentType("image/png");
    OutputStream os = response.getOutputStream();
    ImageIO.write(codeService.getImage(session), "png", os);
}

From source file:org.mitre.discovery.view.WebfingerView.java

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) {

    response.setContentType("application/jrd+json");

    HttpStatus code = (HttpStatus) model.get(HttpCodeView.CODE);
    if (code == null) {
        code = HttpStatus.OK; // default to 200
    }/*from   w  w  w.j a v a2 s.  c o  m*/

    response.setStatus(code.value());

    try {

        String resource = (String) model.get("resource");
        String issuer = (String) model.get("issuer");

        JsonObject obj = new JsonObject();
        obj.addProperty("subject", resource);

        JsonArray links = new JsonArray();
        JsonObject link = new JsonObject();
        link.addProperty("rel", "http://openid.net/specs/connect/1.0/issuer");
        link.addProperty("href", issuer);
        links.add(link);

        obj.add("links", links);

        Writer out = response.getWriter();
        gson.toJson(obj, out);

    } catch (IOException e) {

        logger.error("IOException in JsonEntityView.java: ", e);

    }
}

From source file:org.valens.bamboo.servlets.LoggingServlet.java

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    response.setContentType("text/html;charset=utf-8");
    StringBuffer s = new StringBuffer();
    for (String aux : log)
        s.append(aux);//w  w  w .jav a 2 s  .c  om
    response.getWriter().append(s);
}

From source file:com.antelink.sourcesquare.server.servlet.StatusServlet.java

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) {
    try {/*from w  w w . ja v  a  2  s .c  om*/
        Gson gson = new Gson();
        response.setContentType("application/json;charset=utf-8");
        int displayedFilesScanned = computeDisplayedFilesScanned();

        ScanStatus.INSTANCE.setDisplayedFilesScanned(displayedFilesScanned);
        ScanStatus status = ScanStatus.INSTANCE;
        response.getWriter().append(gson.toJson(status));
        logger.trace("replying application advance status");
    } catch (IOException e) {
        logger.debug("Error dispatching status", e);
    }
}

From source file:nl.tue.gale.tools.servlets.ExportFile.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String filename = request.getParameter("fileName");
    String author = request.getParameter("author");
    boolean created = false;
    try {//  www .jav a  2  s.  c om
        String graphauthorfiles = (String) AHAStatic.AUTHORFILESPATH;
        String aharoot = (String) AHAStatic.config(getServletContext()).Get("AHAROOT");
        String fileUrl = aharoot + graphauthorfiles.substring(1) + author + "/" + filename + ".aha";
        created = copyToConfig(fileUrl, filename);
    } catch (Exception e) {
        e.printStackTrace();
    }
    out.println(created);
}

From source file:org.kemri.wellcome.controller.ReportDefinitionController.java

@RequestMapping(value = Views.EXPORT, method = RequestMethod.GET)
public void export(HttpServletResponse response) throws Exception {
    response.setContentType("application/xml");
    response.setCharacterEncoding("UTF-8");
    response.addHeader("Content-Disposition", "attachment; filename=ReportDefinition.xml");
    ReportTemplates templates = service.getReportTemplates();
    if (templates != null)
        service.marshallReportTemplates(response.getOutputStream(), templates);
}