View Javadoc

1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  package org.apache.mina.example.imagine.step1.server;
21  
22  import org.apache.mina.common.IoHandlerAdapter;
23  import org.apache.mina.common.IoSession;
24  import org.apache.mina.example.imagine.step1.ImageRequest;
25  import org.apache.mina.example.imagine.step1.ImageResponse;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import java.awt.Color;
30  import java.awt.Font;
31  import java.awt.Graphics;
32  import java.awt.image.BufferedImage;
33  
34  /**
35   * server-side {@link org.apache.mina.common.IoHandler}
36   *
37   * @author The Apache MINA Project (dev@mina.apache.org)
38   * @version $Rev$, $Date$
39   */
40  
41  public class ImageServerIoHandler extends IoHandlerAdapter {
42  
43      private final static String characters = "mina rocks abcdefghijklmnopqrstuvwxyz0123456789";
44  
45      public static final String INDEX_KEY = ImageServerIoHandler.class.getName() + ".INDEX";
46  
47      private Logger logger = LoggerFactory.getLogger(this.getClass());
48  
49      public void sessionOpened(IoSession session) throws Exception {
50          session.setAttribute(INDEX_KEY, 0);
51      }
52  
53      public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
54          logger.warn(cause.getMessage(), cause);
55      }
56  
57      public void messageReceived(IoSession session, Object message) throws Exception {
58          ImageRequest request = (ImageRequest) message;
59          String text1 = generateString(session, request.getNumberOfCharacters());
60          String text2 = generateString(session, request.getNumberOfCharacters());
61          BufferedImage image1 = createImage(request, text1);
62          BufferedImage image2 = createImage(request, text2);
63          ImageResponse response = new ImageResponse(image1, image2);
64          session.write(response);
65      }
66  
67      private BufferedImage createImage(ImageRequest request, String text) {
68          BufferedImage image = new BufferedImage(request.getWidth(), request.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
69          Graphics graphics = image.createGraphics();
70          graphics.setColor(Color.YELLOW);
71          graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
72          Font serif = new Font("serif", Font.PLAIN, 30);
73          graphics.setFont(serif);
74          graphics.setColor(Color.BLUE);
75          graphics.drawString(text, 10, 50);
76          return image;
77      }
78  
79      private String generateString(IoSession session, int length) {
80          Integer index = (Integer) session.getAttribute(INDEX_KEY);
81          StringBuffer buffer = new StringBuffer(length);
82          while (buffer.length() < length) {
83              buffer.append(characters.charAt(index));
84              index++;
85              if (index >= characters.length()) {
86                  index = 0;
87              }
88          }
89          session.setAttribute(INDEX_KEY, index);
90          return buffer.toString();
91      }
92  
93  }