-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug 568632 Implement feature grant acquire/release
Extract typed EObject from bytes reading for reuse Signed-off-by: eparovyshnaya <[email protected]>
- Loading branch information
1 parent
685e2a3
commit 1759f9a
Showing
5 changed files
with
129 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
....passage.lic.hc/src/org/eclipse/passage/lic/internal/hc/remote/impl/EObjectFromBytes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 ArSysOp | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0/. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* ArSysOp - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.passage.lic.internal.hc.remote.impl; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.emf.ecore.resource.Resource; | ||
import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl; | ||
import org.eclipse.passage.lic.internal.api.LicensingException; | ||
import org.eclipse.passage.lic.internal.hc.i18n.AccessMessages; | ||
|
||
/** | ||
* Reads xmi content from raw byte array and retrieve the single root element of | ||
* the expected type. Throws {@code LicensingException} is case of any surprise. | ||
*/ | ||
public final class EObjectFromBytes<T> { | ||
|
||
private final byte[] content; | ||
private final Class<T> cls; | ||
|
||
public EObjectFromBytes(byte[] content, Class<T> cls) { | ||
this.content = content; | ||
this.cls = cls; | ||
} | ||
|
||
public T get() throws LicensingException { | ||
return from(only(content())); | ||
} | ||
|
||
private List<EObject> content() throws LicensingException { | ||
Resource resource = new XMIResourceImpl(); | ||
try (InputStream input = new ByteArrayInputStream(content)) { | ||
resource.load(input, Collections.emptyMap()); | ||
} catch (IOException e) { | ||
throw new LicensingException(AccessMessages.XmiToEObject_failed_xmi_read, e); | ||
} | ||
return resource.getContents(); | ||
} | ||
|
||
private EObject only(List<EObject> contents) throws LicensingException { | ||
if (contents.size() != 1) { | ||
throw new LicensingException(String.format(AccessMessages.XmiToEObject_unexpected_amount, contents.size())); | ||
} | ||
return contents.get(0); | ||
} | ||
|
||
private T from(EObject only) throws LicensingException { | ||
if (!cls.isInstance(only)) { | ||
throw new LicensingException( | ||
String.format(AccessMessages.XmiToEObject_unexpected_type, only.eClass().getName())); | ||
} | ||
return cls.cast(only); | ||
} | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
...ge.lic.hc/src/org/eclipse/passage/lic/internal/hc/remote/impl/EObjectFromXmiResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 ArSysOp | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0/. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* ArSysOp - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.passage.lic.internal.hc.remote.impl; | ||
|
||
import org.eclipse.passage.lic.internal.api.LicensingException; | ||
import org.eclipse.passage.lic.internal.api.conditions.mining.ContentType; | ||
import org.eclipse.passage.lic.internal.hc.i18n.AccessMessages; | ||
import org.eclipse.passage.lic.internal.hc.remote.ResponseHandler; | ||
|
||
public final class EObjectFromXmiResponse<T> implements ResponseHandler<T> { | ||
|
||
private final Class<T> expected; | ||
|
||
public EObjectFromXmiResponse(Class<T> expected) { | ||
this.expected = expected; | ||
} | ||
|
||
@Override | ||
public T read(byte[] raw, String contentType) throws LicensingException { | ||
// TODO we use `transport`s for Conditions, | ||
// design transports here too should the need arise | ||
contentTypeIsExpected(contentType); | ||
return new EObjectFromBytes<>(raw, expected).get(); | ||
} | ||
|
||
private void contentTypeIsExpected(String contentType) throws LicensingException { | ||
if (!new ContentType.Xml().contentType().equals(contentType)) { | ||
throw new LicensingException(String.format(AccessMessages.EObjectFromXmiResponse_unexpected_content_type, | ||
contentType, new ContentType.Xml().contentType())); | ||
} | ||
} | ||
|
||
} |