com.dv.meetmefor.ws.controller.BinaryDataContoller.java Source code

Java tutorial

Introduction

Here is the source code for com.dv.meetmefor.ws.controller.BinaryDataContoller.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.dv.meetmefor.ws.controller;

import com.dv.meetmefor.model.dao.BinaryDataDao;
import com.dv.meetmefor.model.entity.BinaryData;
import com.dv.meetmefor.shared.Response;
import com.dv.meetmefor.transfer.obj.BinaryDataTransferObject;
import java.io.Serializable;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import org.apache.commons.codec.binary.Base64;

/**
 *
 * @author pp76575
 */
@Stateless
public class BinaryDataContoller extends BaseController<BinaryData> implements Serializable {

    @EJB
    private BinaryDataDao binaryDataDao;

    public BinaryDataContoller() {
        super(BinaryData.class);
    }

    public String upload(String encodedData) {
        byte[] data = Base64.decodeBase64(encodedData);
        Response<Integer> response = new Response();
        BinaryData binaryData = new BinaryData();
        binaryData.setData(data);
        binaryData = binaryDataDao.create(binaryData);
        response.setPayload(binaryData.getId_value().toString());
        response.setResponseType(Response.ResponseType.Info);
        return response.toJson();
    }

    public String download(String id) {
        Integer idInt = Integer.valueOf(id);
        BinaryData binaryData = binaryDataDao.find(idInt);
        byte[] data = Base64.encodeBase64(binaryData.getData());
        return new String(data);
    }

    @Override
    public boolean requiresTransferObject() {
        return false;
    }

}