Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions asterisk-java-core/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
plugins {
id 'java-library'
}

java {
sourceCompatibility = '17'
targetCompatibility = '17'

withSourcesJar()
withJavadocJar()
}

repositories {
mavenCentral()
}

dependencies {
testImplementation 'org.assertj:assertj-core:3.24.2'
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.1'
testImplementation 'org.mockito:mockito-core:5.7.0'
}

tasks.named('test') {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2004-2023 Asterisk Java contributors
*
* 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.asteriskjava.core;

/**
* Newline delimiters used for determine how lines was sent/received to/from Asterisk.
*
* @author Piotr Olaszewski
* @since 4.0.0
*/
public enum NewlineDelimiter {
/**
* AGI uses <b>LF</b> (Line Feed) as a newline delimiter.
*/
LF("\n"),

/**
* AMI uses <b>CRLF</b> (Carriage Return + Line Feed) as a newline delimiter.
*/
CRLF("\r\n"),
;

private final String pattern;

NewlineDelimiter(String pattern) {
this.pattern = pattern;
}

public String getPattern() {
return pattern;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2004-2023 Asterisk Java contributors
*
* 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.asteriskjava.core.databind;

import org.asteriskjava.core.NewlineDelimiter;

/**
* @author Piotr Olaszewski
* @since 4.0.0
*/
public class AsteriskGenerator {
private static final String FIELD_NAME_VALUE_DELIMITER = ": ";

private final StringBuilder stringBuilder = new StringBuilder();

private final NewlineDelimiter newlineDelimiter;

public AsteriskGenerator(NewlineDelimiter newlineDelimiter) {
this.newlineDelimiter = newlineDelimiter;
}

public void writeFieldName(String name) {
stringBuilder.append(name);
stringBuilder.append(FIELD_NAME_VALUE_DELIMITER);
}

public void writeFieldValue(String value) {
stringBuilder.append(value);
stringBuilder.append(newlineDelimiter.getPattern());
}

public String generate() {
return stringBuilder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright 2004-2023 Asterisk Java contributors
*
* 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.asteriskjava.core.databind;

import org.asteriskjava.core.NewlineDelimiter;
import org.asteriskjava.core.databind.writer.AsteriskObjectMethodWriter;
import org.asteriskjava.core.databind.writer.AsteriskObjectWriter;

import java.util.Comparator;
import java.util.List;

import static java.util.Objects.requireNonNull;
import static org.asteriskjava.core.NewlineDelimiter.CRLF;
import static org.asteriskjava.core.NewlineDelimiter.LF;

/**
* @author Piotr Olaszewski
* @since 4.0.0
*/
public class AsteriskObjectMapper {
private final NewlineDelimiter newlineDelimiter;
private final Comparator<String> fieldNamesComparator;

private AsteriskObjectMapper(
NewlineDelimiter newlineDelimiter,
Comparator<String> fieldNamesComparator
) {
this.newlineDelimiter = newlineDelimiter;
this.fieldNamesComparator = fieldNamesComparator;
}

public String writeValue(Object value) {
Class<?> clazz = value.getClass();

AsteriskObjectWriter asteriskObjectWriter = new AsteriskObjectWriter(clazz, fieldNamesComparator);

return writeValue(value, asteriskObjectWriter);
}

private String writeValue(Object value, AsteriskObjectWriter asteriskObjectWriter) {
AsteriskGenerator asteriskGenerator = new AsteriskGenerator(newlineDelimiter);
List<AsteriskObjectMethodWriter> asteriskObjectMethodWriters = asteriskObjectWriter.getAsteriskObjectMethodWriters();
for (AsteriskObjectMethodWriter asteriskObjectMethodWriter : asteriskObjectMethodWriters) {
asteriskObjectMethodWriter.writeName(asteriskGenerator);
asteriskObjectMethodWriter.writeValue(value, asteriskGenerator);
}
return asteriskGenerator.generate();
}

public static Builder builder() {
return new Builder();
}

public static class Builder {
private NewlineDelimiter newlineDelimiter = CRLF;
private Comparator<String> fieldNamesComparator;

public Builder newlineDelimiter(NewlineDelimiter newlineDelimiter) {
this.newlineDelimiter = requireNonNull(newlineDelimiter, "newlineDelimiter cannot be null");
return this;
}

public Builder crlfNewlineDelimiter() {
return newlineDelimiter(CRLF);
}

public Builder lfNewlineDelimiter() {
return newlineDelimiter(LF);
}

public Builder fieldNamesComparator(Comparator<String> fieldNamesComparator) {
this.fieldNamesComparator = requireNonNull(fieldNamesComparator, "fieldNamesComparator cannot be null");
return this;
}

public AsteriskObjectMapper build() {
return new AsteriskObjectMapper(newlineDelimiter, fieldNamesComparator);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2004-2023 Asterisk Java contributors
*
* 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.asteriskjava.core.databind.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Marker annotation that can be used to define a logical property name.
*
* @author Piotr Olaszewski
* @since 4.0.0
*/
@Target({METHOD})
@Retention(RUNTIME)
public @interface AsteriskName {
/**
* Defines the name of the logical property, i.e., the AMI action field name.
*/
String value() default "";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2004-2023 Asterisk Java contributors
*
* 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.asteriskjava.core.databind.annotation;

import org.asteriskjava.core.databind.serializer.AsteriskSerializer;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Annotation used for configuring serialization aspects, by attaching to "getter" methods.
*
* @author Piotr Olaszewski
* @since 4.0.0
*/
@Target({METHOD})
@Retention(RUNTIME)
public @interface AsteriskSerialize {
/**
* Serializer class to use for serializing associated value.
*/
Class<? extends AsteriskSerializer<?>> value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2004-2023 Asterisk Java contributors
*
* 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.asteriskjava.core.databind.serializer;

import org.asteriskjava.core.databind.AsteriskGenerator;

/**
* Interface representing a serializer for a given type.
*
* @param <T> type of the serialized value
* @author Piotr Olaszewski
* @since 4.0.0
*/
public interface AsteriskSerializer<T> {
/**
* Serializes object into Asterisk string.
*
* @param fieldName field name of the currently serialized object
* @param value object to serialize
* @param asteriskGenerator generator used to write a Java object to an Asterisk string
*/
void serialize(String fieldName, T value, AsteriskGenerator asteriskGenerator);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2004-2023 Asterisk Java contributors
*
* 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.asteriskjava.core.databind.serializer;

import org.asteriskjava.core.databind.AsteriskGenerator;

/**
* Marker interface for writing the field name in the serializer implementation instead of {@link AsteriskGenerator}.
*
* @author Piotr Olaszewski
* @since 4.0.0
*/
public interface WritableFileName {
}
Loading