werecloud.api.view.StringView.java Source code

Java tutorial

Introduction

Here is the source code for werecloud.api.view.StringView.java

Source

/*
  werecloud provides a simple API to the VMWare vCenter data.
  Copyright (C) 2015  NigelB
    
  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.
    
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
    
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

package werecloud.api.view;

import org.springframework.web.servlet.View;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

public class StringView implements View {
    @Override
    public String getContentType() {
        return "test/plain";
    }

    @Override
    public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        if (model.containsKey("string")) {
            String data = model.get("string").toString();
            byte[] _data = data.getBytes();
            response.setContentType(getContentType());
            response.setContentLength(_data.length);
            ServletOutputStream out = response.getOutputStream();
            out.write(_data);
            out.flush();
            out.close();
            return;
        }
        throw new Exception("Could not find model.");
    }
}