/*
 * Copyright 2013-2015 the original author or authors.
 *
 * 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.
 */

package org.springframework.cloud.lattice.connector;

import org.springframework.cloud.service.common.AmqpServiceInfo;

import io.pivotal.receptor.commands.ActualLRPResponse;

/**
 * @author Spencer Gibb
 */
public class AmqpServiceInfoCreator extends LatticeServiceInfoCreator<AmqpServiceInfo> {

	public AmqpServiceInfoCreator() {
		super("rabbit");
	}

	@Override
	public AmqpServiceInfo createServiceInfo(Process process) {
		ActualLRPResponse actual = process.getFirstActual();
		String address = actual.getAddress();
		int port = actual.getPorts()[0].getHostPort();
		String username = "guest";
		String password = "guest";
		String virtualHost = "";
		return new AmqpServiceInfo(actual.getInstanceGuid(), address, port, username, password, virtualHost);
	}

}

@@ -789,16 +789,63 @@
 esponse;
+%0Aimport org.springframework.cloud.util.UriInfo;
 %0A%0A/**%0A *
@@ -1319,10 +1319,12 @@
 t = 
-%22%22
+null
 ;%0A%09%09
@@ -1334,16 +1334,23 @@
 urn new 
+Lattice
 AmqpServ
@@ -1436,10 +1436,1176 @@
 t);%0A%09%7D%0A%0A
-%7D
 %0A
+%09private static class LatticeAmqpServiceInfo extends AmqpServiceInfo %7B%0A%09%09public LatticeAmqpServiceInfo(String id, String host, int port, String username, String password, String virtualHost) %7B%0A%09%09%09super(id, host, port, username, password, virtualHost);%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09protected UriInfo validateAndCleanUriInfo(UriInfo uriInfo) %7B%0A%09%09%09if (uriInfo.getScheme() == null) %7B%0A%09%09%09%09throw new IllegalArgumentException(%22Missing scheme in amqp URI: %22 + uriInfo);%0A%09%09%09%7D%0A%0A%09%09%09if (uriInfo.getHost() == null) %7B%0A%09%09%09%09throw new IllegalArgumentException(%22Missing authority in amqp URI: %22 + uriInfo);%0A%09%09%09%7D%0A%0A%09%09%09if (uriInfo.getUserName() == null %7C%7C uriInfo.getPassword() == null) %7B%0A%09%09%09%09throw new IllegalArgumentException(%22Missing userinfo in amqp URI: %22 + uriInfo);%0A%09%09%09%7D%0A%0A%09%09%09String path = uriInfo.getPath();%0A%09%09%09if (path == null) %7B%0A%09%09%09%09//NO-OP, this is the default vhost%0A%09%09%09%7D else %7B%0A%09%09%09%09// Check that the path only has a single segment.  As we have an authority component%0A%09%09%09%09// in the URI, paths always begin with a slash.%0A%09%09%09%09if (path.indexOf('/') != -1) %7B%0A%09%09%09%09%09throw new IllegalArgumentException(%22Multiple segments in path of amqp URI: %22 + uriInfo);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09return uriInfo;%0A%09%09%7D%0A%09%7D%0A%0A%7D

work around default vhost bug in AmqpServiceInfo