Java tutorial
/** * Mule Development Kit * Copyright 2010-2011 (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com * * 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. */ /** * This file was automatically generated by the Mule Development Kit */ package org.mule.modules.bitly; import java.net.URI; import java.net.URLEncoder; import java.util.Collections; import java.util.HashMap; import java.util.Map; import javax.annotation.PostConstruct; import org.apache.commons.codec.EncoderException; import org.apache.commons.codec.net.URLCodec; import org.mule.api.annotations.Module; import org.mule.api.ConnectionException; import org.mule.api.annotations.Configurable; import org.mule.api.annotations.Processor; import org.mule.api.annotations.param.Default; import org.mule.api.annotations.param.Optional; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.client.RestTemplate; /** * Generic module * * @author MuleSoft, Inc. */ @Module(name = "bitly", schemaVersion = "1.0-SNAPSHOT") public class MuleBitlyModule { private final Logger log = LoggerFactory.getLogger(getClass()); private final String FORMAT = "json"; private final String BITLY_API = "https://api-ssl.bitly.com/v3"; private final String BITLY_API_SHORTEN = BITLY_API + "/shorten?login={login}&apiKey={apiKey}&longUrl={longUrl}&format={format}"; private final URLCodec codec = new URLCodec(); private Map<String, String> PARAMS; private RestTemplate restTemplate; @Configurable private String login; @Configurable private String apiKey; public String getLogin() { return login; } public void setLogin(String login) { this.login = login; } public String getApiKey() { return apiKey; } public void setApiKey(String apiKey) { this.apiKey = apiKey; } public void setRestTemplate(RestTemplate template) { this.restTemplate = template; } @PostConstruct public void postConstruct() { if (null == this.restTemplate) { this.restTemplate = new RestTemplate(); } Map<String, String> p = new HashMap<String, String>(); p.put("login", getLogin()); p.put("apiKey", getApiKey()); p.put("format", FORMAT); this.PARAMS = Collections.unmodifiableMap(p); } /** * Custom processor * * {@sample.xml ../../../doc/MuleBitly-connector.xml.sample mulebitly:my-processor} * * @param content Content to be processed * @return Some string */ @Processor public String shorten(URI uri) { log.info("Shortening {}", uri); Map<String, String> params = new HashMap<String, String>(PARAMS); params.put("longUrl", uri.toString()); Map<String, Object> res = restTemplate.getForObject( BITLY_API + "/shorten?login={login}&apiKey={apiKey}&longUrl={longUrl}&format={format}", Map.class, params); log.debug("Response from bit.ly: {}", res); if (!(res.get("status_code").equals(200))) { throw new RuntimeException( String.format("Error with bit.ly - {} {}", res.get("status_code"), res.get("status_txt"))); } return ((Map<String, String>) (res.get("data"))).get("url"); } }