http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/io/brooklyn/camp/rest/util/WebResourceUtils.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/io/brooklyn/camp/rest/util/WebResourceUtils.java b/camp/camp-server/src/main/java/io/brooklyn/camp/rest/util/WebResourceUtils.java deleted file mode 100644 index b0c3554..0000000 --- a/camp/camp-server/src/main/java/io/brooklyn/camp/rest/util/WebResourceUtils.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 io.brooklyn.camp.rest.util; - -import io.brooklyn.camp.dto.ApiErrorDto; - -import java.net.URI; - -import javax.ws.rs.WebApplicationException; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriInfo; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class WebResourceUtils { - - private static final Logger log = LoggerFactory.getLogger(WebResourceUtils.class); - - public static WebApplicationException notFound(String format, Object... args) { - String msg = String.format(format, args); - if (log.isDebugEnabled()) log.debug("returning 404 notFound("+msg+")"); - throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND) - .type(MediaType.APPLICATION_JSON_TYPE) - .entity(ApiErrorDto.builder().message(msg).build()).build()); - } - - public static WebApplicationException preconditionFailed(String format, Object... args) { - String msg = String.format(format, args); - if (log.isDebugEnabled()) log.debug("returning 412 preconditionFailed("+msg+")"); - throw new WebApplicationException(Response.status(Response.Status.PRECONDITION_FAILED) - .type(MediaType.APPLICATION_JSON_TYPE) - .entity(ApiErrorDto.builder().message(msg).build()).build()); - } - - public static Response created(UriInfo info, String resourceUriPath) { - // see http://stackoverflow.com/questions/13702481/javax-response-prepends-method-path-when-setting-location-header-path-on-status - // for why we have to return absolute path - URI resourceUri = info.getBaseUriBuilder().path( resourceUriPath ).build(); - return Response.created(resourceUri).build(); - } - -}
http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/ApiErrorDto.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/ApiErrorDto.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/ApiErrorDto.java new file mode 100644 index 0000000..a65dbe1 --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/ApiErrorDto.java @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.dto; + +import static com.google.common.base.Preconditions.checkNotNull; + +import org.codehaus.jackson.annotate.JsonProperty; + +import com.google.common.base.Objects; +import com.google.common.base.Optional; +import com.google.common.base.Throwables; + +/** + * A simple error message that provides a message and optional details. + * + * This class should eventually be replaced with an ErrorMessage object, + * as described in the CAMP spec. + */ +public class ApiErrorDto { + + public static Builder builder() { + return new Builder(); + } + + /** + * @return An {@link ApiErrorDto.Builder} whose message is initialised to either the throwable's + * message or the throwable's class name if the message is null and whose details are + * initialised to the throwable's stack trace. + */ + public static Builder fromThrowable(Throwable t) { + checkNotNull(t, "throwable"); + String message = Optional.fromNullable(t.getMessage()) + .or(t.getClass().getName()); + return builder() + .message(message) + .details(Throwables.getStackTraceAsString(t)); + } + + public static class Builder { + private String message; + private String details; + + public Builder message(String message) { + this.message = checkNotNull(message, "message"); + return this; + } + + public Builder details(String details) { + this.details = checkNotNull(details, "details"); + return this; + } + + public ApiErrorDto build() { + return new ApiErrorDto(message, details); + } + + public Builder fromApiErrorDto(ApiErrorDto error) { + return this + .message(error.message) + .details(error.details); + } + } + + private final String message; + private final String details; + + public ApiErrorDto( + @JsonProperty("message") String message, + @JsonProperty("details") String details) { + this.message = checkNotNull(message, "message"); + this.details = details != null ? details : ""; + } + + public String getMessage() { + return message; + } + + public String getDetails() { + return details; + } + + @Override + public boolean equals(Object other) { + if (this == other) return true; + if (other == null || getClass() != other.getClass()) return false; + ApiErrorDto that = ApiErrorDto.class.cast(other); + return Objects.equal(this.message, that.message) && + Objects.equal(this.details, that.details); + } + + @Override + public int hashCode() { + return Objects.hashCode(message, details); + } + + @Override + public String toString() { + return Objects.toStringHelper(this) + .add("message", message) + .add("details", details) + .toString(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/ApplicationComponentDto.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/ApplicationComponentDto.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/ApplicationComponentDto.java new file mode 100644 index 0000000..f18787b --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/ApplicationComponentDto.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.dto; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.brooklyn.camp.server.rest.util.DtoFactory; +import org.apache.brooklyn.camp.spi.ApplicationComponent; +import org.apache.brooklyn.camp.spi.Link; +import org.apache.brooklyn.camp.spi.PlatformComponent; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +public class ApplicationComponentDto extends ResourceDto { + + // defined as a constant so can be used in Swagger REST API annotations + public static final String CLASS_NAME = "org.apache.brooklyn.camp.server.dto.ApplicationComponentDto"; + static { assert CLASS_NAME.equals(ApplicationComponentDto.class.getCanonicalName()); } + + protected ApplicationComponentDto() {} + protected ApplicationComponentDto(DtoFactory dtoFactory, ApplicationComponent x) { + super(dtoFactory, x); + + platformComponents = new ArrayList<LinkDto>(); + for (Link<PlatformComponent> t: x.getPlatformComponents().links()) { + platformComponents.add(LinkDto.newInstance(dtoFactory, PlatformComponent.class, t)); + } + + applicationComponents = new ArrayList<LinkDto>(); + for (Link<ApplicationComponent> t: x.getApplicationComponents().links()) { + applicationComponents.add(LinkDto.newInstance(dtoFactory, ApplicationComponent.class, t)); + } + } + + private List<LinkDto> platformComponents; + private List<LinkDto> applicationComponents; + + @JsonInclude(Include.NON_EMPTY) + public List<LinkDto> getPlatformComponents() { + return platformComponents; + } + + @JsonInclude(Include.NON_EMPTY) + public List<LinkDto> getApplicationComponents() { + return applicationComponents; + } + + // --- building --- + + public static ApplicationComponentDto newInstance(DtoFactory dtoFactory, ApplicationComponent x) { + return new ApplicationComponentDto(dtoFactory, x); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/ApplicationComponentTemplateDto.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/ApplicationComponentTemplateDto.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/ApplicationComponentTemplateDto.java new file mode 100644 index 0000000..b522eb4 --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/ApplicationComponentTemplateDto.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.dto; + +import org.apache.brooklyn.camp.server.rest.util.DtoFactory; +import org.apache.brooklyn.camp.spi.ApplicationComponentTemplate; + +public class ApplicationComponentTemplateDto extends ResourceDto { + + // defined as a constant so can be used in Swagger REST API annotations + public static final String CLASS_NAME = "org.apache.brooklyn.camp.server.dto.ApplicationComponentTemplateDto"; + static { assert CLASS_NAME.equals(ApplicationComponentTemplateDto.class.getCanonicalName()); } + + protected ApplicationComponentTemplateDto() {} + protected ApplicationComponentTemplateDto(DtoFactory dtoFactory, ApplicationComponentTemplate x) { + super(dtoFactory, x); + // TODO set addl ACT fields + } + + // TODO add addl ACT fields + + // --- building --- + + public static ApplicationComponentTemplateDto newInstance(DtoFactory dtoFactory, ApplicationComponentTemplate x) { + return new ApplicationComponentTemplateDto(dtoFactory, x); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/AssemblyDto.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/AssemblyDto.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/AssemblyDto.java new file mode 100644 index 0000000..0e6723e --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/AssemblyDto.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.dto; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.brooklyn.camp.server.rest.util.DtoFactory; +import org.apache.brooklyn.camp.spi.ApplicationComponent; +import org.apache.brooklyn.camp.spi.Assembly; +import org.apache.brooklyn.camp.spi.Link; +import org.apache.brooklyn.camp.spi.PlatformComponent; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +public class AssemblyDto extends ResourceDto { + + // defined as a constant so can be used in Swagger REST API annotations + public static final String CLASS_NAME = "org.apache.brooklyn.camp.server.dto.AssemblyDto"; + static { assert CLASS_NAME.equals(AssemblyDto.class.getCanonicalName()); } + + protected AssemblyDto() {} + protected AssemblyDto(DtoFactory dtoFactory, Assembly x) { + super(dtoFactory, x); + + platformComponents = new ArrayList<LinkDto>(); + for (Link<PlatformComponent> t: x.getPlatformComponents().links()) { + platformComponents.add(LinkDto.newInstance(dtoFactory, PlatformComponent.class, t)); + } + + applicationComponents = new ArrayList<LinkDto>(); + for (Link<ApplicationComponent> t: x.getApplicationComponents().links()) { + applicationComponents.add(LinkDto.newInstance(dtoFactory, ApplicationComponent.class, t)); + } + } + + private List<LinkDto> platformComponents; + private List<LinkDto> applicationComponents; + + // TODO addl AssemblyTemplate fields +// "parameterDefinitionUri": URI, +// "pdpUri" : URI ? + + @JsonInclude(Include.NON_EMPTY) + public List<LinkDto> getPlatformComponents() { + return platformComponents; + } + + @JsonInclude(Include.NON_EMPTY) + public List<LinkDto> getApplicationComponents() { + return applicationComponents; + } + + // --- building --- + + public static AssemblyDto newInstance(DtoFactory dtoFactory, Assembly x) { + return new AssemblyDto(dtoFactory, x); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/AssemblyTemplateDto.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/AssemblyTemplateDto.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/AssemblyTemplateDto.java new file mode 100644 index 0000000..6413403 --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/AssemblyTemplateDto.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.dto; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.brooklyn.camp.server.rest.util.DtoFactory; +import org.apache.brooklyn.camp.spi.ApplicationComponentTemplate; +import org.apache.brooklyn.camp.spi.AssemblyTemplate; +import org.apache.brooklyn.camp.spi.Link; +import org.apache.brooklyn.camp.spi.PlatformComponentTemplate; + +public class AssemblyTemplateDto extends ResourceDto { + + // defined as a constant so can be used in Swagger REST API annotations + public static final String CLASS_NAME = "org.apache.brooklyn.camp.server.dto.AssemblyTemplateDto"; + static { assert CLASS_NAME.equals(AssemblyTemplateDto.class.getCanonicalName()); } + + protected AssemblyTemplateDto() {} + protected AssemblyTemplateDto(DtoFactory dtoFactory, AssemblyTemplate x) { + super(dtoFactory, x); + + platformComponentTemplates = new ArrayList<LinkDto>(); + for (Link<PlatformComponentTemplate> t: x.getPlatformComponentTemplates().links()) { + platformComponentTemplates.add(LinkDto.newInstance(dtoFactory, PlatformComponentTemplate.class, t)); + } + + applicationComponentTemplates = new ArrayList<LinkDto>(); + for (Link<ApplicationComponentTemplate> t: x.getApplicationComponentTemplates().links()) { + applicationComponentTemplates.add(LinkDto.newInstance(dtoFactory, ApplicationComponentTemplate.class, t)); + } + } + + private List<LinkDto> platformComponentTemplates; + private List<LinkDto> applicationComponentTemplates; + + // TODO addl AssemblyTemplate fields +// "parameterDefinitionUri": URI, +// "pdpUri" : URI ? + + public List<LinkDto> getPlatformComponentTemplates() { + return platformComponentTemplates; + } + + public List<LinkDto> getApplicationComponentTemplates() { + return applicationComponentTemplates; + } + + // --- building --- + + public static AssemblyTemplateDto newInstance(DtoFactory dtoFactory, AssemblyTemplate x) { + return new AssemblyTemplateDto(dtoFactory, x); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/DtoBase.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/DtoBase.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/DtoBase.java new file mode 100644 index 0000000..fbf48ca --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/DtoBase.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.dto; + +import org.apache.commons.lang3.builder.EqualsBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; +import org.apache.commons.lang3.builder.ToStringBuilder; + +public class DtoBase { + + @Override public String toString() { return ToStringBuilder.reflectionToString(this); } + @Override public boolean equals(Object obj) { return EqualsBuilder.reflectionEquals(this, obj); } + @Override public int hashCode() { return HashCodeBuilder.reflectionHashCode(this); } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/DtoCustomAttributes.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/DtoCustomAttributes.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/DtoCustomAttributes.java new file mode 100644 index 0000000..ad9c5bd --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/DtoCustomAttributes.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.dto; + +import java.util.Map; + +import brooklyn.util.collections.MutableMap; + +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.google.common.collect.ImmutableMap; + +public class DtoCustomAttributes extends DtoBase { + + private Map<String,Object> customAttributes = new MutableMap<String, Object>(); + + protected DtoCustomAttributes() {} + + public DtoCustomAttributes(Map<String,?> customAttributes) { + this.customAttributes = customAttributes==null ? ImmutableMap.<String, Object>of() : ImmutableMap.copyOf(customAttributes); + } + + @JsonIgnore + public Map<String, Object> getCustomAttributes() { + return customAttributes; + } + + // --- json --- + + @JsonInclude(Include.NON_EMPTY) + @JsonAnyGetter + private Map<String,Object> any() { + return customAttributes; + } + @JsonAnySetter + private void set(String name, Object value) { + customAttributes.put(name, value); + } + + // --- building --- + + protected void newInstanceCustomAttributes(Map<String,?> customAttributes) { + if (customAttributes!=null) + this.customAttributes.putAll(customAttributes); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/LinkDto.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/LinkDto.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/LinkDto.java new file mode 100644 index 0000000..d12de65 --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/LinkDto.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.dto; + +import java.util.Map; + +import org.apache.brooklyn.camp.server.rest.util.DtoFactory; +import org.apache.brooklyn.camp.spi.AbstractResource; +import org.apache.brooklyn.camp.spi.Link; + +public class LinkDto extends DtoCustomAttributes { + + // defined as a constant so can be used in Swagger REST API annotations + public static final String CLASS_NAME = "org.apache.brooklyn.camp.server.dto.LinkDto"; + static { assert CLASS_NAME.equals(LinkDto.class.getCanonicalName()); } + + private String href; + private String targetName; + + protected LinkDto() {} + + public String getHref() { + return href; + } + + public String getTargetName() { + return targetName; + } + + // --- building --- + + public static LinkDto newInstance(DtoFactory dtoFactory, Class<? extends AbstractResource> targetType, Link<?> x) { + return new LinkDto().newInstanceInitialization(dtoFactory, targetType, x); + } + + protected LinkDto newInstanceInitialization(DtoFactory dtoFactory, Class<? extends AbstractResource> targetType, Link<?> x) { + targetName = x.getName(); + + href = dtoFactory.uri(targetType, x.getId()); + return this; + } + + public static LinkDto newInstance(String href, String targetName) { + LinkDto x = new LinkDto(); + x.href = href; + x.targetName = targetName; + return x; + } + + public static LinkDto newInstance(String href, String targetName, Map<String,?> customAttributes) { + LinkDto x = newInstance(href, targetName); + x.newInstanceCustomAttributes(customAttributes); + return x; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/PlatformComponentDto.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/PlatformComponentDto.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/PlatformComponentDto.java new file mode 100644 index 0000000..446f9b5 --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/PlatformComponentDto.java @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.dto; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.brooklyn.camp.server.rest.util.DtoFactory; +import org.apache.brooklyn.camp.spi.ApplicationComponent; +import org.apache.brooklyn.camp.spi.Link; +import org.apache.brooklyn.camp.spi.PlatformComponent; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +public class PlatformComponentDto extends ResourceDto { + + // defined as a constant so can be used in Swagger REST API annotations + public static final String CLASS_NAME = "org.apache.brooklyn.camp.server.dto.PlatformComponentDto"; + static { assert CLASS_NAME.equals(PlatformComponentDto.class.getCanonicalName()); } + + protected PlatformComponentDto() {} + protected PlatformComponentDto(DtoFactory dtoFactory, PlatformComponent x) { + super(dtoFactory, x); + setExternalManagementUri(x.getExternalManagementUri()); + platformComponents = new ArrayList<LinkDto>(); + for (Link<PlatformComponent> t: x.getPlatformComponents().links()) { + platformComponents.add(LinkDto.newInstance(dtoFactory, PlatformComponent.class, t)); + } + + applicationComponents = new ArrayList<LinkDto>(); + for (Link<ApplicationComponent> t: x.getApplicationComponents().links()) { + applicationComponents.add(LinkDto.newInstance(dtoFactory, ApplicationComponent.class, t)); + } + } + + private List<LinkDto> platformComponents; + private List<LinkDto> applicationComponents; + + private String externalManagementUri; + + @JsonInclude(Include.NON_EMPTY) + public List<LinkDto> getPlatformComponents() { + return platformComponents; + } + + @JsonInclude(Include.NON_EMPTY) + public List<LinkDto> getApplicationComponents() { + return applicationComponents; + } + + @JsonInclude(Include.NON_EMPTY) + public String getExternalManagementUri() { + return externalManagementUri; + } + private void setExternalManagementUri(String externalManagementUri) { + this.externalManagementUri = externalManagementUri; + } + + // --- building --- + + public static PlatformComponentDto newInstance(DtoFactory dtoFactory, PlatformComponent x) { + return new PlatformComponentDto(dtoFactory, x); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/PlatformComponentTemplateDto.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/PlatformComponentTemplateDto.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/PlatformComponentTemplateDto.java new file mode 100644 index 0000000..1de7cbf --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/PlatformComponentTemplateDto.java @@ -0,0 +1,44 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.dto; + +import org.apache.brooklyn.camp.server.rest.util.DtoFactory; +import org.apache.brooklyn.camp.spi.PlatformComponentTemplate; + +public class PlatformComponentTemplateDto extends ResourceDto { + + // defined as a constant so can be used in Swagger REST API annotations + public static final String CLASS_NAME = "org.apache.brooklyn.camp.server.dto.PlatformComponentTemplateDto"; + static { assert CLASS_NAME.equals(PlatformComponentTemplateDto.class.getCanonicalName()); } + + protected PlatformComponentTemplateDto() {} + protected PlatformComponentTemplateDto(DtoFactory dtoFactory, PlatformComponentTemplate x) { + super(dtoFactory, x); + // TODO set addl PCT fields + } + + // TODO add addl PCT fields + + // --- building --- + + public static PlatformComponentTemplateDto newInstance(DtoFactory dtoFactory, PlatformComponentTemplate x) { + return new PlatformComponentTemplateDto(dtoFactory, x); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/PlatformDto.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/PlatformDto.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/PlatformDto.java new file mode 100644 index 0000000..db52964 --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/PlatformDto.java @@ -0,0 +1,131 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.dto; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.brooklyn.camp.server.rest.resource.ApidocRestResource; +import org.apache.brooklyn.camp.server.rest.util.DtoFactory; +import org.apache.brooklyn.camp.spi.ApplicationComponent; +import org.apache.brooklyn.camp.spi.ApplicationComponentTemplate; +import org.apache.brooklyn.camp.spi.Assembly; +import org.apache.brooklyn.camp.spi.AssemblyTemplate; +import org.apache.brooklyn.camp.spi.Link; +import org.apache.brooklyn.camp.spi.PlatformComponent; +import org.apache.brooklyn.camp.spi.PlatformComponentTemplate; +import org.apache.brooklyn.camp.spi.PlatformRootSummary; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; + +public class PlatformDto extends ResourceDto { + + // defined as a constant so can be used in Swagger REST API annotations + public static final String CLASS_NAME = "org.apache.brooklyn.camp.server.dto.PlatformDto"; + static { assert CLASS_NAME.equals(PlatformDto.class.getCanonicalName()); } + + protected PlatformDto() {} + protected PlatformDto(DtoFactory dtoFactory, PlatformRootSummary x) { + super(dtoFactory, x); + platformComponentTemplates = new ArrayList<LinkDto>(); + for (Link<PlatformComponentTemplate> t: dtoFactory.getPlatform().platformComponentTemplates().links()) { + platformComponentTemplates.add(LinkDto.newInstance(dtoFactory, PlatformComponentTemplate.class, t)); + } + + applicationComponentTemplates = new ArrayList<LinkDto>(); + for (Link<ApplicationComponentTemplate> t: dtoFactory.getPlatform().applicationComponentTemplates().links()) { + applicationComponentTemplates.add(LinkDto.newInstance(dtoFactory, ApplicationComponentTemplate.class, t)); + } + + assemblyTemplates = new ArrayList<LinkDto>(); + for (Link<AssemblyTemplate> t: dtoFactory.getPlatform().assemblyTemplates().links()) { + assemblyTemplates.add(LinkDto.newInstance(dtoFactory, AssemblyTemplate.class, t)); + } + + platformComponents = new ArrayList<LinkDto>(); + for (Link<PlatformComponent> t: dtoFactory.getPlatform().platformComponents().links()) { + platformComponents.add(LinkDto.newInstance(dtoFactory, PlatformComponent.class, t)); + } + + applicationComponents = new ArrayList<LinkDto>(); + for (Link<ApplicationComponent> t: dtoFactory.getPlatform().applicationComponents().links()) { + applicationComponents.add(LinkDto.newInstance(dtoFactory, ApplicationComponent.class, t)); + } + + assemblies = new ArrayList<LinkDto>(); + for (Link<Assembly> t: dtoFactory.getPlatform().assemblies().links()) { + assemblies.add(LinkDto.newInstance(dtoFactory, Assembly.class, t)); + } + + // TODO set custom fields + + apidoc = LinkDto.newInstance( + dtoFactory.getUriFactory().uriOfRestResource(ApidocRestResource.class), + "API documentation"); + } + + // TODO add custom fields + private List<LinkDto> assemblyTemplates; + private List<LinkDto> platformComponentTemplates; + private List<LinkDto> applicationComponentTemplates; + private List<LinkDto> assemblies; + private List<LinkDto> platformComponents; + private List<LinkDto> applicationComponents; + + // non-CAMP, but useful + private LinkDto apidoc; + + public List<LinkDto> getAssemblyTemplates() { + return assemblyTemplates; + } + + public List<LinkDto> getPlatformComponentTemplates() { + return platformComponentTemplates; + } + + public List<LinkDto> getApplicationComponentTemplates() { + return applicationComponentTemplates; + } + + public List<LinkDto> getAssemblies() { + return assemblies; + } + + @JsonInclude(Include.NON_EMPTY) + public List<LinkDto> getPlatformComponents() { + return platformComponents; + } + + @JsonInclude(Include.NON_EMPTY) + public List<LinkDto> getApplicationComponents() { + return applicationComponents; + } + + public LinkDto getApidoc() { + return apidoc; + } + + // --- building --- + + public static PlatformDto newInstance(DtoFactory dtoFactory, PlatformRootSummary x) { + return new PlatformDto(dtoFactory, x); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/ResourceDto.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/ResourceDto.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/ResourceDto.java new file mode 100644 index 0000000..83987ff --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/dto/ResourceDto.java @@ -0,0 +1,112 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.dto; + +import java.util.Date; +import java.util.List; + +import org.apache.brooklyn.camp.commontypes.RepresentationSkew; +import org.apache.brooklyn.camp.server.rest.util.DtoFactory; +import org.apache.brooklyn.camp.spi.AbstractResource; + +import brooklyn.util.time.Time; + +import com.fasterxml.jackson.annotation.JsonGetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonSetter; +import com.fasterxml.jackson.databind.util.ISO8601Utils; + +public class ResourceDto extends DtoCustomAttributes { + + protected ResourceDto() {} + protected ResourceDto(DtoFactory dtoFactory, AbstractResource x) { + type = x.getType(); + name = x.getName(); + + description = x.getDescription(); + setCreated(x.getCreated()); + tags = x.getTags(); + representationSkew = x.getRepresentationSkew(); + + if (x.getCustomAttributes()!=null && !x.getCustomAttributes().isEmpty()) + newInstanceCustomAttributes(x.getCustomAttributes()); + + uri = dtoFactory.uri(x); + } + + private String uri; + private String type; + + private String name; + private String description; + private Date created; + private List<String> tags; + private RepresentationSkew representationSkew; + + public String getUri() { + return uri; + } + + public String getName() { + return name; + } + + @JsonInclude(Include.NON_NULL) + public String getDescription() { + return description; + } + + @JsonGetter("created") + public String getCreatedAsString() { + return created==null ? null : ISO8601Utils.format(created); + } + + @JsonSetter + private void setCreated(Date created) { + this.created = Time.dropMilliseconds(created); + } + + @JsonIgnore + public Date getCreated() { + return created; + } + + @JsonInclude(Include.NON_EMPTY) + public List<String> getTags() { + return tags; + } + + public String getType() { + return type; + } + + @JsonInclude(Include.NON_NULL) + public RepresentationSkew getRepresentationSkew() { + return representationSkew; + } + + // --- building --- + + public static ResourceDto newInstance(DtoFactory dtoFactory, AbstractResource x) { + return new ResourceDto(dtoFactory, x); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/CampRestResources.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/CampRestResources.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/CampRestResources.java new file mode 100644 index 0000000..2d3030c --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/CampRestResources.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.rest; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.brooklyn.camp.server.rest.resource.AbstractCampRestResource; +import org.apache.brooklyn.camp.server.rest.resource.ApidocRestResource; +import org.apache.brooklyn.camp.server.rest.resource.ApplicationComponentRestResource; +import org.apache.brooklyn.camp.server.rest.resource.ApplicationComponentTemplateRestResource; +import org.apache.brooklyn.camp.server.rest.resource.AssemblyRestResource; +import org.apache.brooklyn.camp.server.rest.resource.AssemblyTemplateRestResource; +import org.apache.brooklyn.camp.server.rest.resource.PlatformComponentRestResource; +import org.apache.brooklyn.camp.server.rest.resource.PlatformComponentTemplateRestResource; +import org.apache.brooklyn.camp.server.rest.resource.PlatformRestResource; +import org.apache.brooklyn.rest.apidoc.ApidocHelpMessageBodyWriter; + +import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider; +import com.google.common.collect.Iterables; + +public class CampRestResources { + + public static Iterable<AbstractCampRestResource> getCampRestResources() { + List<AbstractCampRestResource> resources = new ArrayList<AbstractCampRestResource>(); + resources.add(new PlatformRestResource()); + resources.add(new AssemblyTemplateRestResource()); + resources.add(new PlatformComponentTemplateRestResource()); + resources.add(new ApplicationComponentTemplateRestResource()); + resources.add(new AssemblyRestResource()); + resources.add(new PlatformComponentRestResource()); + resources.add(new ApplicationComponentRestResource()); + return resources; + } + + public static Iterable<Object> getApidocResources() { + List<Object> resources = new ArrayList<Object>(); + resources.add(new ApidocHelpMessageBodyWriter()); + resources.add(new ApidocRestResource()); + return resources; + } + + public static Iterable<Object> getMiscResources() { + List<Object> resources = new ArrayList<Object>(); + resources.add(new JacksonJsonProvider()); + return resources; + } + + public static Iterable<Object> getAllResources() { + return Iterables.concat(getCampRestResources(), getApidocResources(), getMiscResources()); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/CampServer.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/CampServer.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/CampServer.java new file mode 100644 index 0000000..1ad931b --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/CampServer.java @@ -0,0 +1,185 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.rest; + +import java.io.File; +import java.io.IOException; +import java.util.EnumSet; + +import javax.servlet.DispatcherType; + +import org.apache.brooklyn.camp.CampPlatform; +import org.apache.brooklyn.camp.server.rest.resource.PlatformRestResource; +import org.apache.brooklyn.camp.server.rest.util.DtoFactory; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.ContextHandler; +import org.eclipse.jetty.servlet.FilterHolder; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.util.thread.QueuedThreadPool; +import org.eclipse.jetty.webapp.WebAppContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import brooklyn.util.exceptions.Exceptions; +import brooklyn.util.net.Networking; + +import com.google.common.base.Charsets; +import com.google.common.io.Files; +import com.sun.jersey.api.core.DefaultResourceConfig; +import com.sun.jersey.api.core.ResourceConfig; +import com.sun.jersey.spi.container.servlet.ServletContainer; + +public class CampServer { + + private static final Logger log = LoggerFactory.getLogger(CampServer.class); + + public static final String CAMP_PLATFORM_ATTRIBUTE = CampPlatform.class.getCanonicalName(); + public static final String DTO_FACTORY = DtoFactory.class.getCanonicalName(); + + private final CampPlatform platform; + private final String uriBase; + private DtoFactory dtoFactory; + + WebAppContext webAppContext; + Server server; + + public CampServer(CampPlatform platform, String uriBase) { + this.platform = platform; + this.uriBase = uriBase; + } + + public CampPlatform getPlatform() { + return platform; + } + + public String getUriBase() { + return uriBase; + } + + public WebAppContext getWebAppContext() { + return webAppContext; + } + + public synchronized DtoFactory getDtoFactory() { + if (dtoFactory!=null) return dtoFactory; + dtoFactory = createDtoFactory(); + return dtoFactory; + } + + protected DtoFactory createDtoFactory() { + return new DtoFactory(getPlatform(), getUriBase()); + } + + public synchronized CampServer start() { + if (webAppContext!=null) + throw new IllegalStateException("Already started"); + + webAppContext = new WebAppContext(); + webAppContext.setContextPath("/"); + webAppContext.setAttribute(CAMP_PLATFORM_ATTRIBUTE, getPlatform()); + webAppContext.setAttribute(DTO_FACTORY, getDtoFactory()); + webAppContext.setWar( + // TODO if there is a GUI or other war... + //findJsguiWebapp()!=null ? findJsguiWebapp() : + CampServerUtils.createTempWebDirWithIndexHtml("CAMP REST API <p> (no gui available - " + + "rest endpoint at <a href=\""+PlatformRestResource.CAMP_URI_PATH+"\">"+PlatformRestResource.CAMP_URI_PATH+"</a>)")); + CampServerUtils.installAsServletFilter(webAppContext); + + server = CampServerUtils.startServer(webAppContext, "CAMP server"); + + return this; + } + + public synchronized void stop() { + try { + server.stop(); + server = null; + webAppContext.stop(); + webAppContext = null; + } catch (Exception e) { + throw Exceptions.propagate(e); + } + } + + public Integer getPort() { + if (server==null) return null; + return server.getConnectors()[0].getLocalPort(); + } + + public static class CampServerUtils { + + public static void installAsServletFilter(ServletContextHandler context) { + // TODO security + // installBrooklynPropertiesSecurityFilter(context); + + // now set up the REST servlet resources + ResourceConfig config = new DefaultResourceConfig(); + // load all our REST API modules, JSON, and Swagger + for (Object r: CampRestResources.getAllResources()) + config.getSingletons().add(r); + + // configure to match empty path, or any thing which looks like a file path with /assets/ and extension html, css, js, or png + // and treat that as static content + config.getProperties().put(ServletContainer.PROPERTY_WEB_PAGE_CONTENT_REGEX, "(/?|[^?]*/assets/[^?]+\\.[A-Za-z0-9_]+)"); + + // and anything which is not matched as a servlet also falls through (but more expensive than a regex check?) + config.getFeatures().put(ServletContainer.FEATURE_FILTER_FORWARD_ON_404, true); + + // finally create this as a _filter_ which falls through to a web app or something (optionally) + FilterHolder filterHolder = new FilterHolder(new ServletContainer(config)); + context.addFilter(filterHolder, "/*", EnumSet.allOf(DispatcherType.class)); + } + + public static Server startServer(ContextHandler context, String summary) { + // FIXME port hardcoded + int port = Networking.nextAvailablePort(8080); + Server server = new Server(port); + server.setHandler(context); + + // use a nice name in the thread pool (otherwise this is exactly the same as Server defaults) + QueuedThreadPool threadPool = new QueuedThreadPool(); + threadPool.setName("camp-jetty-server-"+port+"-"+threadPool.getName()); + server.setThreadPool(threadPool); + + try { + server.start(); + } catch (Exception e) { + throw Exceptions.propagate(e); + } + log.info("CAMP REST server started ("+summary+") on"); + log.info(" http://localhost:"+server.getConnectors()[0].getLocalPort()+"/"); + + return server; + } + + /** create a directory with a simple index.html so we have some content being served up */ + public static String createTempWebDirWithIndexHtml(String indexHtmlContent) { + File dir = Files.createTempDir(); + dir.deleteOnExit(); + try { + Files.write(indexHtmlContent, new File(dir, "index.html"), Charsets.UTF_8); + } catch (IOException e) { + Exceptions.propagate(e); + } + return dir.getAbsolutePath(); + } + + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/AbstractCampRestResource.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/AbstractCampRestResource.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/AbstractCampRestResource.java new file mode 100644 index 0000000..ed8a09c --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/AbstractCampRestResource.java @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.rest.resource; + +import javax.servlet.ServletContext; +import javax.ws.rs.core.Context; + +import org.apache.brooklyn.camp.CampPlatform; +import org.apache.brooklyn.camp.server.rest.util.CampRestContext; +import org.apache.brooklyn.camp.server.rest.util.DtoFactory; +import org.apache.brooklyn.camp.server.rest.util.WebResourceUtils; +import org.apache.brooklyn.camp.spi.AbstractResource; +import org.apache.brooklyn.camp.spi.collection.ResourceLookup; + +public abstract class AbstractCampRestResource { + + // can be injected by jersey when not injected manually + // (seems there is no way to make this optional so note it _must_ be injected; if needed + // see notes on workarounds for test frameworks in original AbstractBrooklynRestResource) + @Context ServletContext servletContext; + + private CampRestContext campRestContext; + + public synchronized CampRestContext context() { + if (campRestContext!=null) return campRestContext; + campRestContext = new CampRestContext(servletContext); + return campRestContext; + } + + public CampPlatform camp() { return context().camp(); } + public DtoFactory dto() { return context().dto(); } + + public static <T extends AbstractResource> T lookup(ResourceLookup<T> list, String id) { + T result = list.get(id); + if (result==null) + throw WebResourceUtils.notFound("No such element: %s", id); + return result; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/ApidocRestResource.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/ApidocRestResource.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/ApidocRestResource.java new file mode 100644 index 0000000..398ad17 --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/ApidocRestResource.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.rest.resource; + +import javax.ws.rs.Path; + +import org.apache.brooklyn.rest.apidoc.Apidoc; + +@Path(ApidocRestResource.API_URI_PATH) +@Apidoc("Web API Documentation") +public class ApidocRestResource extends org.apache.brooklyn.rest.apidoc.ApidocResource { + + public static final String API_URI_PATH = PlatformRestResource.CAMP_URI_PATH + "/apidoc"; + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/ApplicationComponentRestResource.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/ApplicationComponentRestResource.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/ApplicationComponentRestResource.java new file mode 100644 index 0000000..8d2f8d1 --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/ApplicationComponentRestResource.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.rest.resource; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; + +import org.apache.brooklyn.camp.server.dto.ApplicationComponentDto; +import org.apache.brooklyn.rest.apidoc.Apidoc; + +import com.wordnik.swagger.core.ApiOperation; +import com.wordnik.swagger.core.ApiParam; + +@Path(ApplicationComponentRestResource.URI_PATH) +@Apidoc("Application Component resources") +@Produces("application/json") +public class ApplicationComponentRestResource extends AbstractCampRestResource { + + public static final String URI_PATH = PlatformRestResource.CAMP_URI_PATH + "/application-components"; + + @Path("/{id}") + @ApiOperation(value = "Get a specific application component", + responseClass = ApplicationComponentDto.CLASS_NAME) + @GET + public ApplicationComponentDto get( + @ApiParam(value = "ID of item being retrieved", required = true) + @PathParam("id") String id) { + return dto().adapt(lookup(camp().applicationComponents(), id)); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/ApplicationComponentTemplateRestResource.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/ApplicationComponentTemplateRestResource.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/ApplicationComponentTemplateRestResource.java new file mode 100644 index 0000000..3ce76e7 --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/ApplicationComponentTemplateRestResource.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.rest.resource; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; + +import org.apache.brooklyn.camp.server.dto.ApplicationComponentTemplateDto; +import org.apache.brooklyn.rest.apidoc.Apidoc; + +import com.wordnik.swagger.core.ApiOperation; +import com.wordnik.swagger.core.ApiParam; + +@Path(ApplicationComponentTemplateRestResource.URI_PATH) +@Apidoc("Application Component Template resources") +@Produces("application/json") +public class ApplicationComponentTemplateRestResource extends AbstractCampRestResource { + + public static final String URI_PATH = PlatformRestResource.CAMP_URI_PATH + "/application-component-templates"; + + @Path("/{id}") + @ApiOperation(value = "Get a specific application component template", + responseClass = ApplicationComponentTemplateDto.CLASS_NAME) + @GET + public ApplicationComponentTemplateDto get( + @ApiParam(value = "ID of item being retrieved", required = true) + @PathParam("id") String id) { + return dto().adapt(lookup(camp().applicationComponentTemplates(), id)); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/AssemblyRestResource.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/AssemblyRestResource.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/AssemblyRestResource.java new file mode 100644 index 0000000..100bbf9 --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/AssemblyRestResource.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.rest.resource; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; + +import org.apache.brooklyn.camp.server.dto.AssemblyDto; +import org.apache.brooklyn.rest.apidoc.Apidoc; + +import com.wordnik.swagger.core.ApiOperation; +import com.wordnik.swagger.core.ApiParam; + +@Path(AssemblyRestResource.URI_PATH) +@Apidoc("Assembly resources") +@Produces("application/json") +public class AssemblyRestResource extends AbstractCampRestResource { + +// private static final Logger log = LoggerFactory.getLogger(AssemblyRestResource.class); + + public static final String URI_PATH = PlatformRestResource.CAMP_URI_PATH + "/assemblies"; + + @Path("/{id}") + @ApiOperation(value = "Get a specific assembly", + responseClass = AssemblyDto.CLASS_NAME) + @GET + public AssemblyDto get( + @ApiParam(value = "ID of item being retrieved", required = true) + @PathParam("id") String id) { + return dto().adapt(lookup(camp().assemblies(), id)); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/AssemblyTemplateRestResource.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/AssemblyTemplateRestResource.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/AssemblyTemplateRestResource.java new file mode 100644 index 0000000..f4a1c91 --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/AssemblyTemplateRestResource.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.rest.resource; + +import java.net.URI; + +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.brooklyn.camp.server.dto.AssemblyTemplateDto; +import org.apache.brooklyn.camp.spi.Assembly; +import org.apache.brooklyn.camp.spi.AssemblyTemplate; +import org.apache.brooklyn.rest.apidoc.Apidoc; + +import brooklyn.util.exceptions.Exceptions; + +import com.wordnik.swagger.core.ApiOperation; +import com.wordnik.swagger.core.ApiParam; + +@Path(AssemblyTemplateRestResource.URI_PATH) +@Apidoc("Assembly Template resources") +@Produces("application/json") +public class AssemblyTemplateRestResource extends AbstractCampRestResource { + + private static final Logger log = LoggerFactory.getLogger(AssemblyTemplateRestResource.class); + + public static final String URI_PATH = PlatformRestResource.CAMP_URI_PATH + "/assembly-templates"; + + @Path("/{id}") + @ApiOperation(value = "Get a specific assembly template", + responseClass = AssemblyTemplateDto.CLASS_NAME) + @GET + public AssemblyTemplateDto get( + @ApiParam(value = "ID of item being retrieved", required = true) + @PathParam("id") String id) { + return dto().adapt(lookup(camp().assemblyTemplates(), id)); + } + + @Path("/{id}") + @ApiOperation(value = "Instantiate a specific assembly template" + // TODO AssemblyDto, or location thereto? +// , responseClass = AssemblyTemplateDto.CLASS_NAME + ) + @POST + public Response post( + @Context UriInfo info, + @ApiParam(value = "ID of item being retrieved", required = true) + @PathParam("id") String id) { + try { + log.info("CAMP REST instantiating AT "+id); + AssemblyTemplate at = lookup(camp().assemblyTemplates(), id); + Assembly assembly = at.getInstantiator().newInstance().instantiate(at, camp()); + // see http://stackoverflow.com/questions/13702481/javax-response-prepends-method-path-when-setting-location-header-path-on-status + // for why we have to return absolute path + URI assemblyUri = info.getBaseUriBuilder().path( dto().adapt(assembly).getUri() ).build(); + return Response.created(assemblyUri).build(); + } catch (Exception e) { + log.error("Unable to create AT "+id+": "+e); + throw Exceptions.propagate(e); + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/PlatformComponentRestResource.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/PlatformComponentRestResource.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/PlatformComponentRestResource.java new file mode 100644 index 0000000..9bc725b --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/PlatformComponentRestResource.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.rest.resource; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; + +import org.apache.brooklyn.camp.server.dto.PlatformComponentDto; +import org.apache.brooklyn.rest.apidoc.Apidoc; + +import com.wordnik.swagger.core.ApiOperation; +import com.wordnik.swagger.core.ApiParam; + +@Path(PlatformComponentRestResource.URI_PATH) +@Apidoc("Platform Component resources") +@Produces("application/json") +public class PlatformComponentRestResource extends AbstractCampRestResource { + + public static final String URI_PATH = PlatformRestResource.CAMP_URI_PATH + "/platform-components"; + + @Path("/{id}") + @ApiOperation(value = "Get a specific platform component", + responseClass = PlatformComponentDto.CLASS_NAME) + @GET + public PlatformComponentDto get( + @ApiParam(value = "ID of item being retrieved", required = true) + @PathParam("id") String id) { + return dto().adapt(lookup(camp().platformComponents(), id)); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/PlatformComponentTemplateRestResource.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/PlatformComponentTemplateRestResource.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/PlatformComponentTemplateRestResource.java new file mode 100644 index 0000000..c3686be --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/PlatformComponentTemplateRestResource.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.rest.resource; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; + +import org.apache.brooklyn.camp.server.dto.PlatformComponentTemplateDto; +import org.apache.brooklyn.rest.apidoc.Apidoc; + +import com.wordnik.swagger.core.ApiOperation; +import com.wordnik.swagger.core.ApiParam; + +@Path(PlatformComponentTemplateRestResource.URI_PATH) +@Apidoc("Platform Component Template resources") +@Produces("application/json") +public class PlatformComponentTemplateRestResource extends AbstractCampRestResource { + + public static final String URI_PATH = PlatformRestResource.CAMP_URI_PATH + "/platform-component-templates"; + + @Path("/{id}") + @ApiOperation(value = "Get a specific platform component template", + responseClass = PlatformComponentTemplateDto.CLASS_NAME) + @GET + public PlatformComponentTemplateDto get( + @ApiParam(value = "ID of item being retrieved", required = true) + @PathParam("id") String id) { + return dto().adapt(lookup(camp().platformComponentTemplates(), id)); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/PlatformRestResource.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/PlatformRestResource.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/PlatformRestResource.java new file mode 100644 index 0000000..16dfa25 --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/resource/PlatformRestResource.java @@ -0,0 +1,87 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.rest.resource; + +import java.io.InputStream; +import java.io.StringReader; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.brooklyn.camp.server.dto.PlatformDto; +import org.apache.brooklyn.camp.server.rest.util.WebResourceUtils; +import org.apache.brooklyn.camp.spi.AssemblyTemplate; +import org.apache.brooklyn.rest.apidoc.Apidoc; + +import com.wordnik.swagger.core.ApiOperation; + +//import io.brooklyn.camp.rest.apidoc.Apidoc; + +@Path(PlatformRestResource.CAMP_URI_PATH) +@Apidoc("Platform (root)") +@Produces("application/json") +public class PlatformRestResource extends AbstractCampRestResource { + + private static final Logger log = LoggerFactory.getLogger(PlatformRestResource.class); + + public static final String CAMP_URI_PATH = "/camp/v11"; + + @ApiOperation(value = "Return the Platform (root) resource", + responseClass = PlatformDto.CLASS_NAME) + @GET + public PlatformDto get() { + return dto().adapt(camp().root()); + } + + @POST + @Consumes({MediaType.APPLICATION_JSON}) + public Response postJson(@Context UriInfo info, String json) { + return postYaml(info, json); + } + + @POST + @Consumes({"application/x-yaml"}) + public Response postYaml(@Context UriInfo info, String yaml) { + log.debug("YAML pdp:\n"+yaml); + AssemblyTemplate template = camp().pdp().registerDeploymentPlan(new StringReader(yaml)); + return created(info, template); + } + + @POST + @Consumes({"application/x-tar", "application/x-tgz", "application/x-zip"}) + public Response postArchive(@Context UriInfo info, InputStream archiveInput) { + log.debug("ARCHIVE pdp"); + AssemblyTemplate template = camp().pdp().registerPdpFromArchive(archiveInput); + return created(info, template); + } + + protected Response created(UriInfo info, AssemblyTemplate template) { + return WebResourceUtils.created(info, dto().adapt(template).getUri()); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/util/CampJsons.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/util/CampJsons.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/util/CampJsons.java new file mode 100644 index 0000000..7c67d81 --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/util/CampJsons.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.rest.util; + +import brooklyn.util.exceptions.Exceptions; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + +public class CampJsons { + + public static String prettyJson(Object o) { + try { + ObjectMapper mapper = new ObjectMapper(); + mapper.enable(SerializationFeature.INDENT_OUTPUT); + return mapper.writeValueAsString(o); + } catch (JsonProcessingException e) { + throw Exceptions.propagate(e); + } + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/util/CampRestContext.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/util/CampRestContext.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/util/CampRestContext.java new file mode 100644 index 0000000..66d6cf8 --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/util/CampRestContext.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.rest.util; + +import javax.servlet.ServletContext; + +import org.apache.brooklyn.camp.CampPlatform; +import org.apache.brooklyn.camp.server.rest.CampServer; + +import com.google.common.base.Preconditions; + +public class CampRestContext { + + private final ServletContext servletContext; + private CampPlatform platform; + private DtoFactory dto; + + public CampRestContext(ServletContext servletContext) { + this.servletContext = servletContext; + } + + public synchronized CampPlatform camp() { + if (platform!=null) return platform; + platform = (CampPlatform) servletContext.getAttribute(CampServer.CAMP_PLATFORM_ATTRIBUTE); + return Preconditions.checkNotNull(platform, "CAMP platform instance not available from ServletContext"); + } + + public DtoFactory dto() { + if (dto!=null) return dto; + dto = (DtoFactory) servletContext.getAttribute(CampServer.DTO_FACTORY); + return Preconditions.checkNotNull(dto, "CAMP DTO factory instance not available from ServletContext"); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/7d782f34/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/util/CampRestGuavas.java ---------------------------------------------------------------------- diff --git a/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/util/CampRestGuavas.java b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/util/CampRestGuavas.java new file mode 100644 index 0000000..22758a4 --- /dev/null +++ b/camp/camp-server/src/main/java/org/apache/brooklyn/camp/server/rest/util/CampRestGuavas.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.brooklyn.camp.server.rest.util; + +import org.apache.brooklyn.camp.spi.AbstractResource; + +import com.google.common.base.Function; + +public class CampRestGuavas { + + public static final Function<AbstractResource,String> IDENTITY_OF_REST_RESOURCE = + new Function<AbstractResource,String>() { + public String apply(AbstractResource input) { return input.getId(); } + }; + +}
