Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove double encoding of the service plan id for requests. Add parsi… #45

Merged
merged 5 commits into from
Sep 18, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Build with Maven
run: mvn --batch-mode --update-snapshots verify -Dgpg.skip
- run: mkdir snapshots && cp target/*.jar snapshots
- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v4
with:
name: Package
path: snapshots
8 changes: 1 addition & 7 deletions src/main/java/com/sinch/xms/ApiConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@
import com.sinch.xms.api.TagsUpdate;
import java.io.Closeable;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -313,19 +311,15 @@ protected void check() {
@Nonnull
private URI endpoint(@Nonnull String subPath, @Nonnull List<NameValuePair> params) {
try {
String spid = URLEncoder.encode(servicePlanId(), "UTF-8");
String path = endpoint().getPath() + "/v1/" + spid + subPath;
String path = endpoint().getPath() + "/v1/" + servicePlanId() + subPath;
thomasf147 marked this conversation as resolved.
Show resolved Hide resolved
URIBuilder uriBuilder = new URIBuilder(endpoint()).setPath(path);

if (!params.isEmpty()) {
uriBuilder.setParameters(params);
}

return uriBuilder.build();
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e);
}
}

Expand Down
84 changes: 84 additions & 0 deletions src/main/java/com/sinch/xms/BadRequestResponseException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*-
* #%L
* SDK for Sinch SMS
* %%
* Copyright (C) 2016 Sinch
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package com.sinch.xms;

import com.sinch.xms.api.BadRequestError;

/**
* Exception representing a non api error response from XMS. This exception is thrown when some
* fundamental contract of the XMS API has been broken.
*
* <p>For information about specific errors please refer to the <a href=
* "https://developers.sinch.com/docs/sms/api-reference/status-codes/">XMS API documentation</a>.
*/
public class BadRequestResponseException extends ApiException {

private static final long serialVersionUID = 1L;

private final Integer status;
private final String path;
private final String timestamp;
private final String error;

BadRequestResponseException(BadRequestError error) {
super(error.path());

this.status = error.status();
this.path = error.path();
this.timestamp = error.timestamp();
this.error = error.error();
}

/**
* The machine readable HTTP status code.
*
* @return the status code.
*/
public Integer getStatus() {
return status;
}

/**
* The path of the request.
*
* @return the path text
*/
public String getPath() {
return path;
}

/**
* The timestamp of the error.
*
* @return the timestamp as a string.
*/
public String getTimestamp() {
return timestamp;
}

/**
* The human readable HTTP error text for the status.
*
* @return the error text
*/
public String getError() {
return error;
}
}
11 changes: 9 additions & 2 deletions src/main/java/com/sinch/xms/EmptyAsyncConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
package com.sinch.xms;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.ValueInstantiationException;
import com.sinch.xms.api.ApiError;
import com.sinch.xms.api.BadRequestError;
import java.io.IOException;
import java.nio.CharBuffer;
import org.apache.http.HttpException;
Expand Down Expand Up @@ -82,8 +84,13 @@ protected Void buildResult(HttpContext context) throws Exception {
return null;
case HttpStatus.SC_BAD_REQUEST:
case HttpStatus.SC_FORBIDDEN:
ApiError error = json.readValue(content, ApiError.class);
throw new ErrorResponseException(error);
try {
ApiError error = json.readValue(content, ApiError.class);
throw new ErrorResponseException(error);
} catch (ValueInstantiationException e) {
BadRequestError error = json.readValue(content, BadRequestError.class);
throw new BadRequestResponseException(error);
}
case HttpStatus.SC_NOT_FOUND:
HttpCoreContext coreContext = HttpCoreContext.adapt(context);
RequestLine rl = coreContext.getRequest().getRequestLine();
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/sinch/xms/JsonApiAsyncConsumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
package com.sinch.xms;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.ValueInstantiationException;
import com.sinch.xms.api.ApiError;
import com.sinch.xms.api.BadRequestError;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -87,8 +89,13 @@ protected T buildResult(HttpContext context) throws Exception {
return null;
case HttpStatus.SC_BAD_REQUEST:
case HttpStatus.SC_FORBIDDEN:
ApiError error = json.readValue(inputStream, ApiError.class);
throw new ErrorResponseException(error);
try {
ApiError error = json.readValue(inputStream, ApiError.class);
throw new ErrorResponseException(error);
} catch (ValueInstantiationException e) {
BadRequestError error = json.readValue(bios.toInputStream(), BadRequestError.class);
throw new BadRequestResponseException(error);
}
case HttpStatus.SC_NOT_FOUND:
HttpCoreContext coreContext = HttpCoreContext.adapt(context);
RequestLine rl = coreContext.getRequest().getRequestLine();
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/sinch/xms/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,15 @@ static <T> T requireNonNull(T o, String name) {
*/
static ConcurrentException unwrapExecutionException(ExecutionException e)
throws ErrorResponseException, UnexpectedResponseException, UnauthorizedException,
NotFoundException {
NotFoundException, BadRequestResponseException {
if (e.getCause() instanceof RuntimeException) {
throw (RuntimeException) e.getCause();
} else if (e.getCause() instanceof Error) {
throw (Error) e.getCause();
} else if (e.getCause() instanceof ErrorResponseException) {
throw (ErrorResponseException) e.getCause();
} else if (e.getCause() instanceof BadRequestResponseException) {
throw (BadRequestResponseException) e.getCause();
} else if (e.getCause() instanceof NotFoundException) {
throw (NotFoundException) e.getCause();
} else if (e.getCause() instanceof UnexpectedResponseException) {
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/com/sinch/xms/api/BadRequestError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*-
* #%L
* SDK for Sinch SMS
* %%
* Copyright (C) 2016 Sinch
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package com.sinch.xms.api;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.sinch.xms.BadRequestResponseException;
import javax.annotation.Nonnull;
import org.immutables.value.Value;

/**
* API object containing an error response.
*
* @see BadRequestResponseException
*/
@Value.Immutable
@ValueStylePackageDirect
@JsonDeserialize(as = BadRequestErrorImpl.class)
public abstract class BadRequestError {

/**
* The timestamp of the error.
*
* @return a non-null string
*/
public abstract String timestamp();

/**
* The machine readable HTTP status code.
*
* @return a non-null integer
*/
public abstract Integer status();

/**
* The human readable HTTP error text for the status.
*
* @return a non-null string
*/
public abstract String error();

/**
* The path of the request.
*
* @return a non-null string
*/
public abstract String path();

/**
* Creates a new bad request error object from the given timestamp, status, error and path.
*
* @param timestamp the timestamp of the error
* @param status the status code
* @param error the error message
* @param path the path
* @return a non-null Bad request error object
*/
@Nonnull
public static BadRequestError of(String timestamp, Integer status, String error, String path) {
return BadRequestErrorImpl.of(timestamp, status, error, path);
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/sinch/xms/api/MoMmsBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ public static final MoMmsBody.Builder builder() {
*/
@Nullable
public abstract String subject();

/**
* The textual message body, if available.
*
* @return the message body or `null` if not available.
*/
@Nullable
public abstract String message();

/**
* The list of attached media.
*
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/sinch/xms/api/MoMmsMedia.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static final MoMmsMedia.Builder builder() {
*/
@Nullable
public abstract String url();

/**
* The content type of provided media. For example, 'image/jpeg'
*
Expand All @@ -67,13 +68,15 @@ public static final MoMmsMedia.Builder builder() {
@Nonnull
@JsonProperty("content_type")
public abstract String contentType();

/**
* The status received while media upload to storage. Possible values are: Uploaded or Failed.
*
* @return the media status
*/
@Nonnull
public abstract MediaStatus status();

/**
* Error code in case of failure or 0 for success.
*
Expand Down
Loading
Loading