com.tlabs.eve.api.server.MessageOfTheDayParser.java Source code

Java tutorial

Introduction

Here is the source code for com.tlabs.eve.api.server.MessageOfTheDayParser.java

Source

package com.tlabs.eve.api.server;

/*
 * #%L
 * This source code is part of the Evanova Android application:
 * https://play.google.com/store/apps/details?id=com.tlabs.android.evanova
 * %%
 * Copyright (C) 2010 - 2012 Evanova (Traquenard Labs)
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */

import java.io.IOException;
import java.util.TimeZone;

import org.apache.commons.lang.StringUtils;

import com.tlabs.eve.api.EveParser;

public class MessageOfTheDayParser implements EveParser<MessageOfTheDayResponse> {

    public MessageOfTheDayResponse parse(byte[] data) throws IOException {
        final int CACHE_IN_MN = /*60 * 8*/5;
        MessageOfTheDayResponse response = new MessageOfTheDayResponse();
        long now = System.currentTimeMillis();
        response.setCachedUntil(now - TimeZone.getDefault().getOffset(now) + CACHE_IN_MN * 60 * 1000);

        String message = new String(data);
        if (message.length() == 0) {
            //keep a content otherwise caching won't occur when the received data is empty
            message = "<br/>";
        } else {
            //The content is not great...we have a MOTD at start (sometimes)
            //or a <center>MOTD</center> or anything with such form
            //Also CCP found it funny to add shellexec: in front of <a> URLs.
            //A real XML would have been great.
            message = StringUtils.removeStart(message, "MOTD");
            message = StringUtils.remove(message, "shellexec:");
            message = StringUtils.replace(message, "<br>", "<br/>");
        }
        response.setMessage(message);
        return response;
    }
}