Skip to content
This repository was archived by the owner on Dec 12, 2018. It is now read-only.

Commit 66b4a40

Browse files
committed
Added error handling for register when password spec is not met.
1 parent 07b6e2f commit 66b4a40

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

impl/src/main/java/com/stormpath/sdk/impl/ds/DefaultDataStore.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.stormpath.sdk.impl.ds.cache.ReadCacheFilter;
3232
import com.stormpath.sdk.impl.ds.cache.WriteCacheFilter;
3333
import com.stormpath.sdk.impl.error.DefaultError;
34+
import com.stormpath.sdk.impl.error.OktaError;
3435
import com.stormpath.sdk.impl.http.CanonicalUri;
3536
import com.stormpath.sdk.impl.http.HttpHeaders;
3637
import com.stormpath.sdk.impl.http.HttpHeadersHolder;
@@ -65,6 +66,7 @@
6566
import org.slf4j.Logger;
6667
import org.slf4j.LoggerFactory;
6768

69+
import javax.servlet.http.HttpServletResponse;
6870
import java.io.InputStream;
6971
import java.io.UnsupportedEncodingException;
7072
import java.net.URLEncoder;
@@ -105,6 +107,8 @@ public class DefaultDataStore implements InternalDataStore {
105107

106108
private static final boolean COLLECTION_CACHING_ENABLED = false; //EXPERIMENTAL - set to true only while developing.
107109

110+
private static boolean oktaEnabled;
111+
108112
private final RequestExecutor requestExecutor;
109113
private final ResourceFactory resourceFactory;
110114
private final MapMarshaller mapMarshaller;
@@ -160,6 +164,12 @@ public DefaultDataStore(RequestExecutor requestExecutor, BaseUrlResolver baseUrl
160164
this.cacheResolver = new DefaultCacheResolver(this.cacheManager, new DefaultCacheRegionNameResolver());
161165
this.apiKeyResolver = apiKeyResolver;
162166

167+
if (baseUrlResolver.getBaseUrl().toLowerCase().contains("okta")) {
168+
oktaEnabled = true;
169+
} else {
170+
oktaEnabled = false;
171+
}
172+
163173
ReferenceFactory referenceFactory = new ReferenceFactory();
164174
this.resourceConverter = new DefaultResourceConverter(referenceFactory);
165175

@@ -600,7 +610,15 @@ private Response execute(Request request) throws ResourceException {
600610
body.put(DefaultError.REQUEST_ID.getName(), requestId);
601611
}
602612

603-
DefaultError error = new DefaultError(body);
613+
com.stormpath.sdk.error.Error error;
614+
if (oktaEnabled) {
615+
OktaError oktaError = new OktaError(body);
616+
// Okta Error response doesn't have status
617+
oktaError.setProperty(OktaError.STATUS.getName(), response.getHttpStatus());
618+
error = oktaError;
619+
} else {
620+
error = new DefaultError(body);
621+
}
604622

605623
throw new ResourceException(error);
606624
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Copyright 2013 Stormpath, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.stormpath.sdk.impl.error;
17+
18+
import com.stormpath.sdk.error.Error;
19+
import com.stormpath.sdk.impl.resource.AbstractResource;
20+
import com.stormpath.sdk.impl.resource.IntegerProperty;
21+
import com.stormpath.sdk.impl.resource.ListProperty;
22+
import com.stormpath.sdk.impl.resource.Property;
23+
import com.stormpath.sdk.impl.resource.StringProperty;
24+
25+
import java.io.Serializable;
26+
import java.util.List;
27+
import java.util.Map;
28+
29+
/**
30+
* @since 0.1
31+
*/
32+
public class OktaError extends AbstractResource implements Error, Serializable {
33+
34+
static final long serialVersionUID = 42L;
35+
36+
public static final IntegerProperty STATUS = new IntegerProperty("status");
37+
static final StringProperty ERROR_CODE = new StringProperty("errorCode");
38+
static final StringProperty ERROR_SUMMARY = new StringProperty("errorSummary");
39+
static final ListProperty ERROR_CAUSES = new ListProperty("errorCauses");
40+
static final StringProperty ERROR_ID = new StringProperty("errorId");
41+
42+
private static final Map<String, Property> PROPERTY_DESCRIPTORS = createPropertyDescriptorMap(
43+
STATUS, ERROR_CODE, ERROR_SUMMARY, ERROR_CAUSES, ERROR_ID
44+
);
45+
46+
public OktaError(Map<String, Object> body) {
47+
super(null, body);
48+
}
49+
50+
// Needed for this class to be serializable
51+
public OktaError() {
52+
super(null, null);
53+
}
54+
55+
@Override
56+
public Map<String, Property> getPropertyDescriptors() {
57+
return PROPERTY_DESCRIPTORS;
58+
}
59+
60+
@Override
61+
public int getStatus() {
62+
return getInt(STATUS);
63+
}
64+
65+
@Override
66+
public int getCode() {
67+
return 0;
68+
}
69+
70+
@Override
71+
@SuppressWarnings("unchecked")
72+
public String getMessage() {
73+
List causes = getListProperty(ERROR_CAUSES.getName());
74+
return ((Map<String, String>)causes.get(0)).get("errorSummary");
75+
}
76+
77+
@Override
78+
public String getDeveloperMessage() {
79+
return "";
80+
}
81+
82+
@Override
83+
public String getMoreInfo() {
84+
return getString(ERROR_SUMMARY);
85+
}
86+
87+
@Override
88+
public String getRequestId() {
89+
return getString(ERROR_ID);
90+
}
91+
92+
}

0 commit comments

Comments
 (0)