001/* 002 * Copyright (C) 2016 Hobrasoft s.r.o. 003 * 004 * This program is free software: you can redistribute it and/or modify 005 * it under the terms of the GNU Affero General Public License as published by 006 * the Free Software Foundation, either version 3 of the License, or 007 * (at your option) any later version. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU Affero General Public License for more details. 013 * 014 * You should have received a copy of the GNU Affero General Public License 015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 016 */ 017package cz.hobrasoft.pdfmu.operation; 018 019import cz.hobrasoft.pdfmu.TextOutput; 020import cz.hobrasoft.pdfmu.WritingMapper; 021import cz.hobrasoft.pdfmu.jackson.Result; 022import cz.hobrasoft.pdfmu.jackson.RpcResponse; 023import java.io.IOException; 024import java.util.logging.Logger; 025 026public abstract class OperationCommon implements Operation { 027 028 private WritingMapper wm = null; 029 protected TextOutput to = new TextOutput(); // Discard messages by default 030 private static final Logger logger = Logger.getLogger(OperationCommon.class.getName()); 031 032 @Override 033 public void setWritingMapper(WritingMapper wm) { 034 this.wm = wm; 035 } 036 037 protected void writeResult(Result result) { 038 // Discard value if mapper was not set 039 if (wm != null) { 040 RpcResponse response = new RpcResponse(result); 041 try { 042 wm.writeValue(response); 043 } catch (IOException ex) { 044 logger.severe(String.format("Cannot write JSON document: %s", ex)); 045 } 046 } 047 } 048 049 @Override 050 public void setTextOutput(TextOutput to) { 051 this.to = to; 052 } 053}