001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.example.imagine.step1.server;
021:
022: import org.apache.mina.common.IoHandlerAdapter;
023: import org.apache.mina.common.IoSession;
024: import org.apache.mina.example.imagine.step1.ImageRequest;
025: import org.apache.mina.example.imagine.step1.ImageResponse;
026: import org.slf4j.Logger;
027: import org.slf4j.LoggerFactory;
028:
029: import java.awt.Color;
030: import java.awt.Font;
031: import java.awt.Graphics;
032: import java.awt.image.BufferedImage;
033:
034: /**
035: * server-side {@link org.apache.mina.common.IoHandler}
036: *
037: * @author The Apache MINA Project (dev@mina.apache.org)
038: * @version $Rev$, $Date$
039: */
040:
041: public class ImageServerIoHandler extends IoHandlerAdapter {
042:
043: private final static String characters = "mina rocks abcdefghijklmnopqrstuvwxyz0123456789";
044:
045: public static final String INDEX_KEY = ImageServerIoHandler.class
046: .getName()
047: + ".INDEX";
048:
049: private Logger logger = LoggerFactory.getLogger(this .getClass());
050:
051: public void sessionOpened(IoSession session) throws Exception {
052: session.setAttribute(INDEX_KEY, 0);
053: }
054:
055: public void exceptionCaught(IoSession session, Throwable cause)
056: throws Exception {
057: logger.warn(cause.getMessage(), cause);
058: }
059:
060: public void messageReceived(IoSession session, Object message)
061: throws Exception {
062: ImageRequest request = (ImageRequest) message;
063: String text1 = generateString(session, request
064: .getNumberOfCharacters());
065: String text2 = generateString(session, request
066: .getNumberOfCharacters());
067: BufferedImage image1 = createImage(request, text1);
068: BufferedImage image2 = createImage(request, text2);
069: ImageResponse response = new ImageResponse(image1, image2);
070: session.write(response);
071: }
072:
073: private BufferedImage createImage(ImageRequest request, String text) {
074: BufferedImage image = new BufferedImage(request.getWidth(),
075: request.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
076: Graphics graphics = image.createGraphics();
077: graphics.setColor(Color.YELLOW);
078: graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
079: Font serif = new Font("serif", Font.PLAIN, 30);
080: graphics.setFont(serif);
081: graphics.setColor(Color.BLUE);
082: graphics.drawString(text, 10, 50);
083: return image;
084: }
085:
086: private String generateString(IoSession session, int length) {
087: Integer index = (Integer) session.getAttribute(INDEX_KEY);
088: StringBuffer buffer = new StringBuffer(length);
089: while (buffer.length() < length) {
090: buffer.append(characters.charAt(index));
091: index++;
092: if (index >= characters.length()) {
093: index = 0;
094: }
095: }
096: session.setAttribute(INDEX_KEY, index);
097: return buffer.toString();
098: }
099:
100: }
|