View Javadoc
1   /* 
2    * Copyright (C) 2016 Hobrasoft s.r.o.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package cz.hobrasoft.pdfmu.operation;
18  
19  import cz.hobrasoft.pdfmu.TextOutput;
20  import cz.hobrasoft.pdfmu.WritingMapper;
21  import cz.hobrasoft.pdfmu.jackson.Result;
22  import cz.hobrasoft.pdfmu.jackson.RpcResponse;
23  import java.io.IOException;
24  import java.util.logging.Logger;
25  
26  public abstract class OperationCommon implements Operation {
27  
28      private WritingMapper wm = null;
29      protected TextOutput to = new TextOutput(); // Discard messages by default
30      private static final Logger logger = Logger.getLogger(OperationCommon.class.getName());
31  
32      @Override
33      public void setWritingMapper(WritingMapper wm) {
34          this.wm = wm;
35      }
36  
37      protected void writeResult(Result result) {
38          // Discard value if mapper was not set
39          if (wm != null) {
40              RpcResponse response = new RpcResponse(result);
41              try {
42                  wm.writeValue(response);
43              } catch (IOException ex) {
44                  logger.severe(String.format("Cannot write JSON document: %s", ex));
45              }
46          }
47      }
48  
49      @Override
50      public void setTextOutput(TextOutput to) {
51          this.to = to;
52      }
53  }