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.core.service.IoHandlerAdapter;
23  import org.apache.mina.core.session.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.core.service.IoHandler}
36   *
37   * @author The Apache MINA Project (dev@mina.apache.org)
38   */
39  
40  public class ImageServerIoHandler extends IoHandlerAdapter {
41  
42      private final static String characters = "mina rocks abcdefghijklmnopqrstuvwxyz0123456789";
43  
44      public static final String INDEX_KEY = ImageServerIoHandler.class.getName() + ".INDEX";
45  
46      private static Logger LOGGER = LoggerFactory.getLogger(ImageServerIoHandler.class);
47  
48      /**
49       * Called when the session is opened, which will come after the session created.
50       * 
51       * @see org.apache.mina.core.service.IoHandlerAdapter#sessionOpened(org.apache.mina.core.session.IoSession)
52       */
53      public void sessionOpened(IoSession session) throws Exception {
54          session.setAttribute(INDEX_KEY, 0);
55      }
56  
57      /**
58       * This method will be called whenever an exception occurs.  For this handler,
59       * the logger will generate a warning message.
60       * 
61       * @see org.apache.mina.core.service.IoHandlerAdapter#exceptionCaught(org.apache.mina.core.session.IoSession, java.lang.Throwable)
62       */
63      public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
64          LOGGER.warn(cause.getMessage(), cause);
65      }
66  
67      /**
68       * Handle incoming messages.
69       * 
70       * @see org.apache.mina.core.service.IoHandlerAdapter#messageReceived(org.apache.mina.core.session.IoSession, java.lang.Object)
71       */
72      public void messageReceived(IoSession session, Object message) throws Exception {
73          ImageRequest request = (ImageRequest) message;
74          String text1 = generateString(session, request.getNumberOfCharacters());
75          String text2 = generateString(session, request.getNumberOfCharacters());
76          BufferedImage image1 = createImage(request, text1);
77          BufferedImage image2 = createImage(request, text2);
78          ImageResponse response = new ImageResponse(image1, image2);
79          session.write(response);
80      }
81  
82      /**
83       * Create an image using the specified request and the text.  
84       *
85       * @param request
86       *  Determines the height and width of the image
87       * @param text
88       *  The text that is placed in the image
89       * @return
90       *  a BufferedImage representing the text.
91       */
92      private BufferedImage createImage(ImageRequest request, String text) {
93          BufferedImage image = new BufferedImage(request.getWidth(), request.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
94          Graphics graphics = image.createGraphics();
95          graphics.setColor(Color.YELLOW);
96          graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
97          Font serif = new Font("serif", Font.PLAIN, 30);
98          graphics.setFont(serif);
99          graphics.setColor(Color.BLUE);
100         graphics.drawString(text, 10, 50);
101         return image;
102     }
103 
104     /**
105      * Generate a string based on the 'characters' field in this class.  The
106      * characters that make up the string are based on the session 
107      * attribute "INDEX_KEY"
108      *
109      * @param session
110      *  The {@link IoSession} object that will provide the INDEX_KEY attribute
111      * @param length
112      *  The length that the String will be
113      * @return
114      *  The generated String
115      */
116     private String generateString(IoSession session, int length) {
117         Integer index = (Integer) session.getAttribute(INDEX_KEY);
118         StringBuffer buffer = new StringBuffer(length);
119         while (buffer.length() < length) {
120             buffer.append(characters.charAt(index));
121             index++;
122             if (index >= characters.length()) {
123                 index = 0;
124             }
125         }
126         session.setAttribute(INDEX_KEY, index);
127         return buffer.toString();
128     }
129 
130 }